Skip to content

Instantly share code, notes, and snippets.

@KlausC
Last active April 27, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KlausC/0354d0d0d09c03430d1da40d621d290e to your computer and use it in GitHub Desktop.
Save KlausC/0354d0d0d09c03430d1da40d621d290e to your computer and use it in GitHub Desktop.
julia syntax of where clause and methods dispatch

Julia extension of where clause and dispatching

Idea is to enable dispatch of Julia methods depending not only on the type (including type parameters), but on special properties of argument types.

As we want to extend the method dispatching system, which is based on type hierarchies, the mentioned properties are expressed as function calls with the type as single argument. The return value of the function must be an abstract or concrete type by itself.

The syntax of the where clause is extended by allowing the type variable to be replaced by a function call to the variable. It may be restricted by upper - and lower bounds.

Syntax

    function foo1(x::T) where {basetype(T)<:SparseMatrixCSC}
    end

    Symmetric{T, P} where {T,basetype(P)<:Array}

Semantics

A where clause of the form where L<:func(T)<:U restricts the lhs to all types T, for which method call func(T) returns a value. to arguments of type Type{T}, and for which L<:func(T)<:U is true. Land U as usually default to Union{} respectively Any.

Restrictions

The used function must be defined before it can be used in a where clause. Its evaluation must be possible at complile time.

Method Dispatching

Just read this discourse request-type-functions-for-parametric-dispatch, which kind of discouraged me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment