f = x -> x + 1
T = typeof(f)
# ==> var"#2#4"
Given a type T
of an anonymous function, can I get that function? I.e. given var#"2#4"
get object f
?
I get the type T
from the lowered representation of code (actually, from IRTools, but it’s nearly the same in this context). E.g.:
@code_lowered (xs -> [x + 1 for x in xs])(rand(5))
# ==> CodeInfo(
# ==> 1 ─ #2 = %new(Main.:(var"#2#4"))
# ==> │ %2 = #2
# ==> │ %3 = Base.Generator(%2, xs)
# ==> │ %4 = Base.collect(%3)
# ==> └── return %4
)
In IR, we have a special instruction %new
which constructs the function, but I can’t find a language or compiler feature to do it from my code.