I have an interface which exposes some functions, let’s call them f
and g
here for the MWE.
The user is expected to define methods for user-defined types. But it is sufficient to define one of them, as the other can be derived. For some types, defining one is more natural than the other, but it varies by type, so there is no default ordering.
I would like to provide an informative error message instead of a StackOverflowError
when the user fails to define either of the methods.
MWE:
Package defines
f(A, x) = g(A, x) + 1
g(A, x) = f(A, x) - 1
User defines
struct MyType end
f(::MyType, x) = abs2(x)
and everything works out:
julia> f(MyType(), 1)
1
julia> g(MyType(), 1)
0
User forgets to define methods
julia> struct MyOtherType end
julia> f(MyOtherType(), 1)
ERROR: StackOverflowError:
Stacktrace:
[1] g(::MyOtherType, ::Int64) at ./REPL[88]:1
[2] f(::MyOtherType, ::Int64) at ./REPL[87]:1
... (the last 2 lines are repeated 39998 more times)
[79999] g(::MyOtherType, ::Int64) at ./REPL[88]:1
I would like to detect this somehow and provide suggestions.