Cannot add methods to abstract type?

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?

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

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)

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

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: WIP: redesign closures, then generic functions by JeffBezanson · Pull Request #13412 · JuliaLang/julia · GitHub

4 Likes