I have an object that stores (amongst other things) a vector of functions. When I use multiple functions, everything works nicely. However, if I pass it a vector with a single function, I’m getting type-inference problems.
Here’s a simplified example:
function times2(i)
2i
end
function times_n(i,n)
i*n
end
This works:
[times2, i -> times_n(i,8)]
And gives
2-element Array{Function,1}:
times2
var"#136#137"()
Which is what I want (would’ve preferred a more informative name but that’s just a matter of convenience).
However, instead if I try:
l = [times2]
push!(l, i -> times_n(i,8))
I get an error:
MethodError: Cannot `convert` an object of type var"#168#169" to an object of type typeof(times2)
Closest candidates are:
convert(::Type{T}, !Matched::T) where T at essentials.jl:171
Stacktrace:
[1] push!(::Array{typeof(times2),1}, ::Function) at ./array.jl:913
[2] top-level scope at In[152]:2
This also happens if I initialise l
with the anonymous function and then try to add times2
.
I’ve tried annotating the relevant expressions with ::Function
but I still get that l
is a list of objects of type typeof(times2)
.
I apologise if this is a trivial question - I’ve tried to search for an answer on the forum but couldn’t find any. Any help would be much appreciated!
8 posts - 4 participants