julia> f = let x = 1
y -> x + y
end
#5 (generic function with 1 method)
julia> (f::typeof(f))(x, y) = x + y - f.x
julia> length(typeof(f).name.mt |> Base.MethodList)
2
Regarding this:
That’s not what’s meant by instance here. Instance is always a matter of storage, never about the number of methods. E.g.
julia> length(methods(+))
208
Yet, + only has one single instance:
julia> typeof(+).instance
+ (generic function with 208 methods)
Rather, what’s important here is that I can make new copies of f that have the same type, but are distinct:
julia> macro new(T, args...)
Expr(:new, T, args...)
end
@new (macro with 1 method)
julia> f2 = @new typeof(f) 2
#5 (generic function with 2 methods)
julia> f2.x
2
julia> typeof(f2) == typeof(f)
true
julia> f2 == f # <--- This is the really important part
false
Now, in reality, you probably shouldn’t be doing this with closures, it’s not really something you’re intended to be doing, but keep in mind that any user defined struct could be a function if you want:
julia> struct Foo <: Function
x::Int
end
julia> (foo::Foo)(x) = foo.x + x
julia> Foo(1)(2)
3