Reputation: 17
I had the following julia function
function fakeseq()
let; global f(x)=unshift!(push!(x, 35) , 1);end; # not working
#let; global f; f=x->unshift!(push!(x, 35) , 1);end; # works fine
s1 = [rand(1:34) for i in 1:12];
s2 = [rand(1:34) for i in 1:7]
data = map(x->f(x), [s1, s2])
end
When I run that code I get the following error:
julia> fakeseq()
ERROR: MethodError: no method matching f(::Array{Int64,1})
The applicable method may be too new: running in world age 21823, while current world is 21824.
Closest candidates are: f(::Any) at REPL[1]:2 (method too new to be called from this world context.)
Stacktrace:
[1] _collect(::Array{Array{Int64,1},1},
::Base.Generator{Array{Array{Int64,1},1},##3#6}, ::Base.EltypeUnknown,
::Base.HasShape) at ./array.jl:488
[2] fakeseq() at ./REPL[1]:6
However, the second definition of f indicated with # works fine
comment works. I couldn't get the exact difference between them, why that happens?
Upvotes: 0
Views: 439
Reputation: 19132
As @AlexanderMorley pointed out, you need to look through
https://docs.julialang.org/en/latest/manual/methods.html#Redefining-Methods-1
or watch Jameson's Youtube video:
https://www.youtube.com/watch?v=7KGZ_9D_DbI
Generic functions are treated somewhat differently than anonymous functions in Julia. In the generic function case, what's going on is something like, the program compiles knowing what f
in the global function table is, you modify f
, but the calling function has already compiled to call what is no longer the correct function and thus it errors. The error is to stop Issue #265. Anonymous functions are different though. In the anonymous function case, the calling function is setup to call whatever function is pointed to by that variable. In that case, it knows it's a global variable too so it's safe and doesn't inline it or anything like that, and so it's successful.
Upvotes: 1