Can I notify a generated function to recompile if called again

Let’s say I have a @generated function of some kind which already triggered compilation at some point given the types of some arguments. Say that something changes in those arguments (fields), and I can catch that this happened before the generated function is called again to try to ask to regenerate itself when called. I think this is forbidden by the manual, though I’d like to ask to some Julia expert if this is somehow still possible given that I control the notification process (maybe I could use Base.delete_method?). I expect that the answer is no or hard and very brittle. Though, It would be useful for some things I could try developing to know more on this.

Maybe you can try redefining the method
It’s not possible to delete methods in julia. You can just overwrite them (even with that they will still be kept in another world age)
So you can do

julia> f(x::MyType) = x.args + 2

julia> f(x) # Assuming there is already a variable x of type MyType

julia> f(x::MyType) = x.args + 2 # The old definition will be replaced by this one

julia> f(x) # this should retrigger compilation 

You can also use functors if it’s the fields of the type that change.
So you could do

(x::MyType)(args...) = do_something(x.args, args...)

How could that be incompatible with the code compiled for the types?

1 Like

ahh right, It can’t really be because you would need to rely on values, not only types in a @generated function to do so, the question is then ill-defined. Thanks for the help.

actually you can, but it is internal and so not advisable to use:

julia> f(x) = 3
f (generic function with 1 method)

julia> Base.delete_method(methods(f)[1])

julia> f(2)
ERROR: MethodError: no method matching f(::Int64)
The function `f` exists, but no method is defined for this combination of argument types.
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1