How to get type stability across methods?

  1. This is specifying that methods more specific than foo(::S) must output Bool. Specificity is a much harder condition to control or detect than subtyping because the multiple positions’s types (a Union of types) don’t guarantee a neat specificity hierarchy even if the individual non-Union types make a neat subtyping hierarchy.
  2. In general, it won’t be feasible for a method definition to throw this error because methods don’t generally have fixed return types or even return types related to the input types via parameters. You could take a page from static typing and force more specific methods to annotate ::Bool or whatever matches the pattern, but you run into the issue with (1) again.

Return type restriction is a feature of statically typed, single-dispatched virtual functions for good reason.

EDIT: I’d probably go with danielwe’s neater approach because you do have the one named function with runtime-varying input types. I was thinking of the other way around, fixing a return type in a 1-method (recall that 1 <= max_methods) wrapper for arbitrary callables over fixed input types. Unlike the similar FunctionWrappers package, improving return type inference this way doesn’t elide the runtime dispatch-associated heap allocations, which is what I made a separate topic to ask about.