Search code examples
juliafunctor

Parametric functors in Julia


Starting 0.6 it is possible to create parametric methods in Julia using where syntax. According to the release notes of 0.6 version, where syntax

can be used anywhere a type is accepted

Now consider the following contrived example:

function (rng::R)() where {R <: Range}
    return first(rng)
end

which, when I attempt to compile it, gives the following error:

ERROR: function type in method definition is not a type

So my question is what is the proper way to create parametric functors in Julia 0.6+?


Solution

  • Ohkay, I get what you are trying to do basically. To understand functors here is a short example code.

    julia> struct Student
               name::String
           end
    
    julia> function (::Student)()
               println("Callable of Student Type!")
           end
    
    julia> object = Student("JuliaLang")
    Student("JuliaLang")
    
    julia> object()
    Callable of Student Type!
    

    but when I try to create the parametric functors, it throws out the error similar to yours!

    julia> function (::T)() where {T <: Student}
               println("Callable of Student Type!")
           end
    ERROR: function type in method definition is not a type
    

    This problem is actually still OPEN as a issue as @gnimuc rightly pointed out.