# Tell the compiler that two code paths yield the same type

**URL:** <https://discourse.julialang.org/t/tell-the-compiler-that-two-code-paths-yield-the-same-type/63535>\
**Category:** General Usage\
**Created:** [June 25, 2021, 8:15am UTC](https://discourse.julialang.org/t/tell-the-compiler-that-two-code-paths-yield-the-same-type/63535 "2021-06-25T08:15:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [June 25, 2021, 8:15am UTC](https://discourse.julialang.org/t/tell-the-compiler-that-two-code-paths-yield-the-same-type/63535/1 "2021-06-25T08:15:33Z")

</div>

Every now and then I have code like this:

```julia
function calculate_or_lookup(...)
    if iscached(...)
        return load_from_cache(...) # cheap
    else
        return calculate(...) # expensive
    end
end

```

As a programmer I know that `calculate` and `load_from_cache` will yield the same type.  
Often the compiler can only infer the type of `calculate` and has no chance to know the return type of `load_from_cache`. Is there a way to tell the compiler that `calculate_or_lookup` has the same return type as `calculate`?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 25, 2021, 8:23am UTC](https://discourse.julialang.org/t/tell-the-compiler-that-two-code-paths-yield-the-same-type/63535/2 "2021-06-25T08:23:29Z")

</div>

> [@jw3126](#):
>
> `load_from_cache(...)`

`load_from_cache(...)::KnownType`

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [June 25, 2021, 8:25am UTC](https://discourse.julialang.org/t/tell-the-compiler-that-two-code-paths-yield-the-same-type/63535/3 "2021-06-25T08:25:33Z")

</div>

Yeah often `KnownType` is a three line long ugly beast with 20 parameters that depend on the input argument types 😃

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 25, 2021, 8:43am UTC](https://discourse.julialang.org/t/tell-the-compiler-that-two-code-paths-yield-the-same-type/63535/4 "2021-06-25T08:43:51Z")

</div>

Does the cache know the type it contains? If so, you can assert the type in the loading function.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 25, 2021, 9:09am UTC](https://discourse.julialang.org/t/tell-the-compiler-that-two-code-paths-yield-the-same-type/63535/5 "2021-06-25T09:09:31Z")

</div>

You could use `Core.Compiler.return_type` for this:

```julia
function calculate_or_lookup(...)
    if iscached(...)
        return load_from_cache(...) # cheap
    else
        return calculate(...)::Core.Compiler.return_type(load_from_cache, Tuple{#= argument types for `load_from_cache` =#})
    end
end

```

although I would first benchmark whether this is actually worth it. Dynamic dispatch is not that expensive as long as it isn’t used in a hot loop, so just adding `@nospecialize` to `calculate_or_lookup` to avoid overly specializing and just relying on dynamic dispatch may well be an option.
