# How to get type stability across methods?

**URL:** <https://discourse.julialang.org/t/how-to-get-type-stability-across-methods/129046>\
**Category:** General Usage\
**Tags:** code\_warntype, type-stability, methods\
**Created:** [May 15, 2025, 6:36pm UTC](https://discourse.julialang.org/t/how-to-get-type-stability-across-methods/129046 "2025-05-15T18:36:32Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 16, 2025, 6:17am UTC](https://discourse.julialang.org/t/how-to-get-type-stability-across-methods/129046/10 "2025-05-16T06:17:11Z")

</div>

> [@akriegman](#):
>
> ```julia
> abstract function foo(::S)::Bool end
> foo(::A) = true
> foo(::B) = 1 # ERROR
> 
> ```

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](https://discourse.julialang.org/t/why-does-runtime-dispatch-allocate-when-the-return-type-is-inferred/129061). 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.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-get-type-stability-across-methods/129046)._
