# Multiple-dispatch depending on output type?

**URL:** <https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639>\
**Category:** Internals & Design\
**Created:** [December 9, 2017, 12:35am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639 "2017-12-09T00:35:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [December 9, 2017, 12:35am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/1 "2017-12-09T00:35:08Z")

</div>

I was just thinking about multiple-dispatch and something just occurred to me. Currently, there is multiple-dispatch based on input types but what if it can dispatch on output type as well?

For example `sqrt(-1)` will fail currently but `sqrt(-1)::Complex` should succeed because `sqrt` is dispatching on output type. One way around this currently is to have the output type as a parameter e.g. `sqrt(-1, outType = Complex)` then it will dispatch based on `Type{Complex}`. For example

```julia
import Base.sqrt
sqrt(x, ::Type{Complex}) = sqrt(Complex(x))
sqrt(-1, Complex) # works fine

```

Of course this a very weird idea as most of the time we write

```julia
x = sqrt(-1)

```

and at this point, x isn’t defined, so it will fail; but

```julia
x::Complex = sqrt(-1)

```

should work if multiple-dispatch is on output type.

I can see how implementing this sort of things will rely on more magic, so may not be a good idea but I think this may be something not many people have talked about. So worth mentioning.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [December 9, 2017, 12:45am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/2 "2017-12-09T00:45:20Z")

</div>

We already do this for some functions, but in the first argument, notably `trunc`, `round`, `floor`, `ceil`.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [December 9, 2017, 12:47am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/3 "2017-12-09T00:47:04Z")

</div>

Ok. Is it available as a language feature or is it defined function by function in the core implementation?

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [December 9, 2017, 12:49am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/4 "2017-12-09T00:49:04Z")

</div>

No, it’s defined function by function. But the nice thing about Julia is that it is easy to add new methods to existing functions!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 9, 2017, 7:38am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/5 "2017-12-09T07:38:23Z")

</div>

Are you talking about `trunc(T, x)` methods? I am not sure this is what @xiaodai was asking about, my understanding is that he would like

```julia
x::Complex = sqrt(-1)

```

to be lowered into a form where `Complex` becomes an argument to `sqrt`.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 9, 2017, 9:40am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/6 "2017-12-09T09:40:03Z")

</div>

If, then it should probably be: `x = sqrt(-1)::Complex`.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 9, 2017, 9:50am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/7 "2017-12-09T09:50:54Z")

</div>

But this is already taken for type assertion syntax.

Also, I am not saying that the original idea is a good one, just trying to interpret what was suggested.

I actually think that making caller context available to functions in this subtle, optional way is not a good design choice. If the function needs that information, it should be explicit in the argument list.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 9, 2017, 9:53am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/8 "2017-12-09T09:53:03Z")

</div>

Haskell dispatches on output type (but I’m not saying Julia should).

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [December 9, 2017, 11:34am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/9 "2017-12-09T11:34:21Z")

</div>

One thing: typically in Julia, when making a method of a function where you want to dispatch on a type, in particular to indicate the return type, it is the first argument, for example, `convert`, `rand`, `zero`, etc., so if you did this, it probably should be something like: `sqrt(Complex, -1)`

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [December 9, 2017, 11:35am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/10 "2017-12-09T11:35:49Z")

</div>

What if your function accepta a function as argument. It that case do we put the type second to allow for the do notation?

---

<div class="post-metadata">

**Author:** ![martinholters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martinholters/32/3690_2.png) [@martinholters](https://discourse.julialang.org/u/martinholters)\
**Post date:** [December 12, 2017, 12:08pm UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/11 "2017-12-12T12:08:01Z")

</div>

[https://github.com/JuliaLang/julia/issues/19206](https://github.com/JuliaLang/julia/issues/19206)

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [October 30, 2024, 4:15pm UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/12 "2024-10-30T16:15:05Z")

</div>

> [@ScottPJones](#):
>
> sqrt(Complex, -1)

Am I missing something?

Why would someone want to write `sqrt(Complex, -1)` when they could instead write `sqrt(Complex(-1))`?

Doesn’t the latter more concisely and accurately describe the calculation you want to perform?

(Maybe this wasn’t a thing in 2017 - I realize by the standards of Julia this is a super old thread.)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 30, 2024, 4:53pm UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/13 "2024-10-30T16:53:19Z")

</div>

Let’s not pick at old comments in old topics, please. We generally keep topics open here because they gather search rank and there could be relevant new information that comes to light, but we don’t need to resurrect threads to just argue about asides. You can see that nobody ran with that suggestion in the seven years since.

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [October 30, 2024, 5:56pm UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/14 "2024-10-30T17:56:22Z")

</div>

> [@mbauman](#):
>
> You can see that nobody ran with that suggestion in the seven years since.

That’s precisely why I am asking the question…

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [October 31, 2024, 8:15am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/15 "2024-10-31T08:15:26Z")

</div>

> [@world-peace](#):
>
> Why would someone want to write `sqrt(Complex, -1)` when they could instead write `sqrt(Complex(-1))`?

Perhaps not with `Complex`, but one can imagine other types where there is overhead in creating `T(-1)` first. E.g. some field where `sqrt` is defined on some embedding of whole numbers.

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [October 31, 2024, 9:37am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/16 "2024-10-31T09:37:27Z")

</div>

> [@sgaure](#):
>
> Perhaps not with `Complex`, but one can imagine other types where there is overhead in creating `T(-1)` first. E.g. some field where `sqrt` is defined on some embedding of whole numbers.

Can you explain this in more detail? I didn’t understand what point you wanted to make here

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [October 31, 2024, 9:52am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/17 "2024-10-31T09:52:55Z")

</div>

So, say you have some algebraic structure `GAlgebra` which allows square roots. Let’s say its representation is somewhat complicated, but the whole numbers can be embedded. You don’t want to write `sqrt(GAlgebra(-1))` because creating `GAlgebra(-1)` is quite involved, and the general `sqrt` for `GAlgebra` is also quite involved. But, due to circumstances, constructing the square root of `GAlgebra(-1)` directly from `-1` (or any other whole number) is simple.

You would likely want something like `sqrt(GAlgebra, -1)`. The typical way in julia would be to make it general, with a fallback like `sqrt(::Type{T}, x) where {T} = sqrt(convert(T, x))`, and allow specializations for structs like `GAlgebra` or `SMatrix{N, N} where N`

---

<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:** [October 31, 2024, 10:30am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/18 "2024-10-31T10:30:18Z")

</div>

> [@world-peace](#):
>
> Why would someone want to write `sqrt(Complex, -1)` when they could instead write `sqrt(Complex(-1))`?

Two reasons:

- 

> overhead in creating `T(-1)` first

- Converting a value or real type to complex is a loss of information, so the `sqrt` method you end up calling may quite possibly be less efficient, because it doesn’t know the imaginary part is zero.

A simple example is `Complex{BigFloat}`. It’s both costly to work with, and not ultimately implemented in Julia (the implementation relies on the venerable MPFR C library). The latter severely restricts the kinds of compiler optimizations that are possible.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [October 31, 2024, 11:55am UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/19 "2024-10-31T11:55:11Z")

</div>

To solve the type instability problem?

---

<div class="post-metadata">

**Author:** ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)\
**Post date:** [October 31, 2024, 12:41pm UTC](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/20 "2024-10-31T12:41:39Z")

</div>

Right I see, to simplify this a bit:

- you want a type `GAlgebra` as an output
- calculating `sqrt(GAlgebra(-1))` would be _slow_ in general because `sqrt(GAlgebra(<anyfloat>))` is some complicated method
- but there is a much simpler implementation `sqrt(GAlgebra(-1))` because `-1::GAlgrbra` is a special case (excuse the abuse of notation)

This is a “runtime” special case. It depends on the _value_ at _runtime_.

Therefore it should be implemented (it could be implemented?) in the function as a branch statement.

```julia
function sqrt(galgebra::GAlgebra)
    if galgebra.something_value == -1
        ...
    else
        ...

```

Julia doesn’t currently provide a way to dispatch methods using values as well as types. Nor (IMO) should it.

I’m aware that’s **not** what is being asked for with a syntax like

```julia
sqrt(GAlgebra, -1)

```

Can you get the same behavior by “overloading” `sqrt` (providing a method for `sqrt`) with a method which takes an argument `type` as the first argument?

```julia
sqrt(some_type, value)
    if some_type isa GAlgebra ...

```

Perhaps this is what was being suggested and I just missed the point being made or otherwise misunderstood here?

[Next page](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639.md?page=2)
