I was playing around with anonymous functions a bit and found that you can use where clauses with them, if you wrap the header in brackets:
julia> ((x::T) where {T<:Int}) -> x
#11 (generic function with 1 method)
julia> ans(5)
5
This clearly means that anonymous functions participate like “normal” functions in multiple dispatch. What I’m wondering then is whether it’s possible to add methods to this same anonymous function. It seems like technically it would be possible, but is there syntax for it?
using Test
anon = x::Int -> x^2
(f::typeof(anon))(x::Float64) = x *2.
@test anon(3) == 9
@test anon(3.) == 6.0
Although why use anonymous function if you are going to end up writing the second line? By writing a second line, you are no longer achieving brevity by using anonymous functions. And by assigning the anonymous function to a variable, you are already polluting the namespace.
The why wasn’t really part of the question, I agree it’s questionable to use this. But I can adapt your example like this:
julia> begin
local anon = x::Int -> x^2
(::typeof(anon))(x::Float64) = x * 2.
anon
end
#2 (generic function with 2 methods)
julia> anon
ERROR: UndefVarError: `anon` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
In which case I can define the anonymous function without polluting the namespace, so that’s nice. But yes, it stops being very concise at this point.
Seems that the standard way to define functions also works for local definitions:
julia> begin
local anon2(x::Int) = x^2
anon2(x::Float64) = 2.0 * x
anon2
end
(::var"#anon2#5") (generic function with 2 methods)
julia> begin
local function anon2(x::Int); x^2 end
function anon2(x::Float64); 2.0 * x end
anon2
end
(::var"#anon2#6") (generic function with 2 methods)
julia> anon2
ERROR: UndefVarError: `anon2` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
The function is there, but it seems the methods are local:
julia> begin
local function anon2(x::Int); x^2 end
function anon2(x::Float64); 2.0 * x end
anon2
end
(::var"#anon2#1") (generic function with 2 methods)
julia> dump(var"#anon2#1")
var"#anon2#1" <: Function
julia> methods(var"#anon2#1")
# 0 methods for type constructor
Interesting, seems to be the same when using an anonymous function though:
julia> begin
local anon = x::Int -> x^2
(::typeof(anon))(x::Float64) = x * 2.
@show typeof(anon)
anon
end
typeof(anon) = var"#7#8"
#7 (generic function with 2 methods)
julia> dump(var"#7#8")
var"#7#8" <: Function
julia> methods(var"#7#8")
# 0 methods for type constructor
Does that mean that every function creates a global struct that is never garbage collected?