Overloading anonymous functions

The below works:

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.

6 Likes