Defining function calls on an object


#1

I am confused about how to define function call syntax for objects.

If I have a concrete type then I can do it:

type MyConcrete end

(c::MyConcrete)(x) = "hello"

julia> cc = MyConcrete()
MyConcrete()

julia> (c::MyConcrete)(x) = "hello $x"

julia> cc(3)
"hello 3"

I thought the following was supposed to work for subtypes of an abstract type

julia> abstract AbstractTest

julia> type MyTest <: AbstractTest end

julia> (t::T){T <: AbstractTest}(x) = "hello"
function type in method definition is not a type

This may be issue https://github.com/JuliaLang/julia/issues/14919, except I am not trying to define call on an abstract type itself, but rather a concrete object whose type is a subtype of a given abstract type.


#2

This is exactly https://github.com/JuliaLang/julia/issues/14919 . Defining AbstractTest() is fine, defining (c::AbstractTest)() is not currently supported.


#3

I am not trying to define (c::AbstractTest), but rather (c::ConcreteTest), for any ConcreteTest that is a subtype of AbstractTest.

EDIT: Or maybe that’s what you meant.


#4

The two defines methods on exactly the same set of types. The error message might be different but none of them are currently supported for the same reason.