I can create an anonymous function within a list comprehension in Julia. I expect that this would create a type of Vector{Function}. Instead, the type is something like Vector{var"#2#4"}.
For example, when involving an outer scope with an anonymous function
typeof(x->x) <: Function # true
a = 1
typeof(x-> x + a) <: Function # false
while
f(x) = x
typeof(f) <: Function # true
a = 1
g(x) = x + a
typeof(g) <: Function # true
Why is the anonymous function typed differently than the regular function when the outer scope is involved?
Now, in a list comprehension:
typeof([x->x, x->x]) <: AbstractVector{Function} # true
typeof([x->x+i for i in 1:2]) <: AbstractVector{Function} # false
typeof([x->x for i in 1:2]) <: AbstractVector{Function} # false
whether or not the index i is involved. I initially expected true in each case.
First note that your initial example is not correct, as you have:
So both anonymous functions have a type that is a subtype of
Function.Now as for comprehension note that this is mostly unrelated with the presence of parameter, as you have:
The reason for this behavior is when you write something like
[...]then Julia picks a narrow element type of the comprehension (if it did not try to do so you would haveAnyelement type - the behavior that Python have for its standard lists).So now how to solve your issue.
To make sure that comprehension has element type of
Functionyou need to write:See also here and here in the manual where it explains how element type for a result of a comprehension and an array literal is picked.
If you omit the
Functionprefix the element type is narrower, and in this case the proper subtyping condition would be as follows:(note the extra
<:).This behavior is related to the fact that in Julia such parametric types are invariant, which is explained here in the Julia manual.