Can one override method ambiguity?

question

#1

I have an API for which the user defines methods for a function, to dispatch on user-defined types.

However, when the second argument is nothing, I want this method to return nothing without the user defining a method for the intersection. MWE:

abstract type ModelType end
struct Bar <: ModelType end
estimate(::Bar, ::Any) = 1
# I want do dispatch to the one below when possible
# WITHOUT DEFINING estimate(::Bar, ::Void)
estimate(::Any, ::Void) = nothing 
estimate(Bar(), nothing)             # this is ambigous

Is this possible somehow?


#2

I constantly run into similar issues, where Julia considers that both are equally specific, so it can’t decide between them, not just with nothing.
For your API, you couldn’t leave off the last argument for the “nothing” case, or is it that it might not be the last argument?


#3

The way i usually work around this is with a function barrier, where the user defines a method for the “inner” function

estimate(a, b) = inner_estimate(a,b)
estimate(a, ::Void) = nothing

example in the wild: https://github.com/Evizero/Augmentor.jl/blob/master/src/operation.jl#L58-L86


#4

This is so useful and elegant that IMO it deserves a place in the documentation.