I finally managed to move some of my codes to v0.6. The World Age problem was a shock as it clashes with one of the main design principles of my code. I’ve read the documentation, and all the discourse topics I could find but I still don’t know how to get around it. Here is a typical code (MWE, the real use case is of course much more complex) :
genfun() = eval( :(r -> exp(r)) ) # generate a julia function (model) from an expression
workwithfun(f) = f(1.0) # do some work with the function (model)
f = genfun()
workwithfun(f)
This works fine, but it fails as soon as I try to execute a function instead of a script, I run into the World Age Problem:
genfun() = eval( :(r -> exp(r)) ) # generate a julia function (model) from an expression
workwithfun(f) = f(1.0) # do some work with the function (model)
function test()
f = genfun()
workwithfun(f)
end
test()
# ERROR: MethodError: no method matching (::##3#4)(::Float64)
# The applicable method may be too new: running in world age 21827, while current world is 21828.
# Closest candidates are:
# #3(::Any) at REPL[12]:1 (method too new to be called from this world context.)
# Stacktrace:
# [1] test() at ./REPL[16]:3
This is in fact exactly the problem described at the beginning of Redefining Methods.
To explain my application: In genfun
I would normally generate a function describing a model symbolically, then differentiate it symbolically, wrap it into a type that specifies other details of the model. This is returned to the caller where the function (model) is then used in various ways. (if anybody is curious, they can look at this issue)
What really struck me is that if I use FunctionWrapper
, then the problem seems to go away?
using FunctionWrappers: FunctionWrapper
genfun() = eval( :(r -> exp(r)) ) |> FunctionWrapper{Float64, Tuple{Float64}}
workwithfun(f) = f(1.0)
function test()
f = genfun()
workwithfun(f)
end
test()
This is probably my best workaround for now, but it comes with (1) non-generic code (maybe this could be fixed though); (2) some performance penalty e.g. no inlining???; (3) it just feels like a hack
Is there no better way to achieve what I want?
CC @ettersi