A generated function I’m writing needs to determine if its allowed to call a function its been passed, or if that would result in a world-age error (in which case it would fall back to something else). As part of this, I suppose I need to get the world-age that the generated function is currently running in and pass that to hasmethod
, but this doesn’t seem to work. Here’s what I’ve tried:
defined_before() = "before"
@generated function try_call(func)
if hasmethod(func.instance, (), world=Base.get_world_counter())
func.instance()
else
"cant call"
end
end
defined_after() = "after"
try_call(defined_before) # returns "before"; good
try_call(defined_after) # should return "cant call", instead MethodErrors
Adding a print statement around the Base.get_world_counter()
inside the generated function reveals that it returns the “current” world-age after everything’s been defined, rather than the world-age inside the generated function (ie I’m guessing this is not what get_world_counter
is meant for):
Int(Base.get_world_counter()) = 29689
ERROR: LoadError: MethodError: no method matching defined_after()
The applicable method may be too new: running in world age 29688, while current world is 29689.
So is there any way to do this, without just trying to call func
and catching the potential MethodError? Alternatively, is there a better way to determine if a function is callable in the current world age?