Closed baggepinnen closed 1 year ago
Good point. Do you think a try ... finally ... end
block, in which the restoration of the original value happens in the finally
section, would solve the problem?
It would probably work as long as the catch block has access to the index at which the failure occurred. try/catch
is expensive in some situations, but I guess that the function being differentiated is typically much more expensive so perhaps it's ont much of a problem.
Yeah, try
-finally
is the correct pattern for this, we should be doing that.
(Possibly in the future we should use the effects to check if a function is side-effect free, then we can avoid the try-finally, and the defensive copy)
Code like this https://github.com/JuliaDiff/FiniteDifferences.jl/blob/c7a4624092d90b6d49d5701967982226231d4478/src/grad.jl#L18
mutates the input argument, but before returning makes sure the input argument is restored to its original value. However, if the user-provided function errors, the input argument is left in a mutated state, which can cause bugs that are very hard to detect.