Cannot add methods to abstract type?

type
error

#1

I’m getting this strange error message in Julia 0.6.1 when I try doing

abstract type MyAbstractType end

function (a::MyAbstractType)()
    println(42)
end

which results in

ERROR: cannot add methods to an abstract type

However everything works fine if I do

function call(a::MyAbstractType)
    println(42)
end

struct MyConcreteType <: MyAbstractType end

obj = MyConcreteType()
call(obj) # prints "42"

Is this expected?


#2

Some relevant reading: https://github.com/JuliaLang/julia/issues/14919


#3

Unfortunately, what you’re asking for in (a::MyAbstractType)() = println(42) is not possible (this was a breaking change from Julia v0.4 to v0.5). Instead you can either create your own function (as you’ve done) or implement the calling syntax for the concrete types:

(a::MyConcreteType)() = println(42)

#4

Oh well, I’m sure that’s for a good cause. Thank you!


#5

Indeed it was! The good cause was that the same change which broke this feature brought us fast, inlined anonymous functions, which in turn brought us all of the magic of super-fast broadcasting and loop fusion: https://github.com/JuliaLang/julia/pull/13412