-
Notifications
You must be signed in to change notification settings - Fork 7
Description
I have a use case that needs to determine if a function has an inverse or not in a type-stable fashion (try/catch won't do). I think such a functionality would be useful in general.
I see two options:
a) Adding a has_inverse(f)
function that returns a Singleton. This would be a breaking change, functions that support inverse
would now be required to support has_inverse
as well.
b) A default implementation inverse(f) = nothing
(or missing
?). This would be simpler (especially if we add left_inverse
and right_inverse
as well later on) and non-breaking. On the other hand, code that depends on functions having an inverse would fail in a less obvious way, the root cause would be harder to track (users would get a MethodError: objects of type Missing are not callable
instead of a MethodError: no method matching inverse(::typeof(f))
). On the other hand, code that uses inverses could check if the inverse is not nothing
to ensure the exception points to the right place, e.g via something(inverse(f))
.
An example for approach b) would be ChainRulesCore (rrule(f, args...)
returns nothing
by default).
@devmotion what do you think?