# Is there a function in Base that determines the most specific type?

**URL:** <https://discourse.julialang.org/t/is-there-a-function-in-base-that-determines-the-most-specific-type/124215>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 27, 2024, 6:32pm UTC](https://discourse.julialang.org/t/is-there-a-function-in-base-that-determines-the-most-specific-type/124215 "2024-12-27T18:32:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tqml](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tqml/32/22710_2.png) [@tqml](https://discourse.julialang.org/u/tqml)\
**Post date:** [December 27, 2024, 6:32pm UTC](https://discourse.julialang.org/t/is-there-a-function-in-base-that-determines-the-most-specific-type/124215/1 "2024-12-27T18:32:14Z")

</div>

I’m currently wondering if there is a function anywhere in Base or the standard library that figures out the most specific type element for a given type (e.g. like multiple dispatch does).

I would like to have a function that receives a vector of type signatures (or a vector of types) and a “target type” and that returns the index of the most specific type

Update: Found something that does would I want in an [earlier post](https://discourse.julialang.org/t/find-most-specific-type/64528).  
Still, is this the best way to do this?

```julia
type_morespecific(a, b) = ccall(:jl_type_morespecific, Cint, (Any, Any), a, b)

function get_most_specific_type(types, target)
    candidates = filter(t -> target <: t, types)
    if isempty(candidates)
        return nothing
    end

    result = sort(candidates, lt = Bool∘type_morespecific)
    return first(result)
end

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 27, 2024, 9:38pm UTC](https://discourse.julialang.org/t/is-there-a-function-in-base-that-determines-the-most-specific-type/124215/2 "2024-12-27T21:38:31Z")

</div>

Some notes:

- there’s the `Base.morespecific` function, which does the `ccall` above, and converts to `Bool`
- I don’t think there’s any supported way of doing what you want, `morespecific` isn’t a public interface
- I guess a `reduce(...)` would be more efficient than `first(sort(...))`
