# Recursive fallback methods error message

**URL:** <https://discourse.julialang.org/t/recursive-fallback-methods-error-message/21534>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 6, 2019, 2:08pm UTC](https://discourse.julialang.org/t/recursive-fallback-methods-error-message/21534 "2019-03-06T14:08:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 6, 2019, 2:08pm UTC](https://discourse.julialang.org/t/recursive-fallback-methods-error-message/21534/1 "2019-03-06T14:08:16Z")

</div>

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

```julia
f(A, x) = g(A, x) + 1
g(A, x) = f(A, x) - 1

```

### User defines

```julia
struct MyType end
f(::MyType, x) = abs2(x)

```

and everything works out:

```julia
julia> f(MyType(), 1)
1

julia> g(MyType(), 1)
0

```

### User forgets to define methods

```julia
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.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [March 6, 2019, 3:25pm UTC](https://discourse.julialang.org/t/recursive-fallback-methods-error-message/21534/2 "2019-03-06T15:25:01Z")

</div>

Here’s a suggestion using a trait that requires the user to make one more definition:

```julia
function defbase end

f(A, x) = f(A,x,defbase(A))
g(A, x) = g(A,x,defbase(A))

f(A, x, ::typeof(g)) = g(A, x) + 1
g(A, x, ::typeof(f)) = f(A, x) - 1

```

and the user defines, e.g.,

```julia
struct MyType end
f(::MyType, x) = abs2(x)
defbase(::MyType) = f

```

If the user forgets either the `f` method or `defbase` (which I guess could have some better name for your particular use) they get a `MethodError` rather than `StackOverflow`.

This requires quadratically many method definitions in your package (n\*(n-1) for n functions), but I’m not sure that can be avoided anyway.
