I have a function which takes a user-defined callable object, say f, which when called with a vector of numbers is expected to return the value f(x) and the gradient \nabla f(x) at x. DiffBase.GradientResult is a natural choice for the result type, and it should be up to the user to decide whether to use forward or reverse AD, or something completely different.
The question: ForwardDiff.gradient! and similar operate by overwriting the same GradientResult structure. But I don’t want results of different calls to f to share any structure, because that would lead to hideous bugs. The way I see it, I have two interface choices:
-
f(x)returns results that may share structure, the consumercopys it. Advantage: even if the user messes up, I am safe. -
f(x)is expected to return non-shared values, taking care of it itself.
Is there a reason one of these fits better into Julia than the other? I am leaning toward (1), since the overhead of copy is the least of my concerns ATM. Or are there other options?