# Promote not working for nothing, while promote\_type works

**URL:** https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028
**Category:** General Usage
**Created:** [July 14, 2020, 9:38am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028 "2020-07-14T09:38:19Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [July 14, 2020, 9:38am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/1 "2020-07-14T09:38:19Z")

</div>

```julia
julia> promote(nothing, 4)
ERROR: promotion of types Nothing and Int64 failed to change any arguments
Stacktrace:
 [1] error(::String, ::String, ::String) at ./error.jl:42
 [2] sametype_error(::Tuple{Nothing,Int64}) at ./promotion.jl:306
 [3] not_sametype(::Tuple{Nothing,Int64}, ::Tuple{Nothing,Int64}) at ./promotion.jl:300
 [4] promote(::Nothing, ::Int64) at ./promotion.jl:283
 [5] top-level scope at none:0

julia> promote(typeof(nothing), typeof(4))
(Nothing, Int64)

```

Is there a reasons why it is not allowed to promote types to a Union type?

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [July 14, 2020, 10:03am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/2 "2020-07-14T10:03:48Z")

</div>

`promote` return values, but there are no values of type `Union{T1, T2}` for `T1 != T2`.

---

<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: [July 14, 2020, 10:30am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/3 "2020-07-14T10:30:55Z")

</div>

What would be the expected result of this promotion in your view?

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [July 15, 2020, 4:55am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/4 "2020-07-15T04:55:45Z")

</div>

I guessed it would return the same values.

```julia
promote(nothing, 4) == (nothing, 4)

```

My imagined usecase is to preallocate an Array with type from `promote_type` and to cast all values with `promote` respectively.

---

<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: [July 15, 2020, 6:18am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/5 "2020-07-15T06:18:59Z")

</div>

Instead of guessing, please read `?promote`:

> If no arguments can be converted, an error is raised.

The motivation is that returning the same values would lead to infinite recursion in fallback methods that try to promote before dispatch.

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [July 15, 2020, 6:26am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/6 "2020-07-15T06:26:48Z")

</div>

Hi @Tamas_Papp please don’t assume bad behaviour. I am feeling slightly offended by your phrasing. Guessing is a really good thing to do and I myself would like to encourage everyone not only to read, but also to think.

So let me apply what you quoted and what I also read before:

- Can `nothing` be converted to `Union{Nothing, Int}`? yes it can
- Can `4` be converted to `Union{Nothing, Int}` yes it can
- can all arguments be converted? yes they can

* * *

The motivation you mentioned is something completely different, and I haven’t known that yet and also couldn’t find it in the documentation. Sounds very interesting.

Can you point out where the infinite recursion would happen if I would enable `promote(nothing, 4) == (nothing, 4)`?

---

<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: [July 15, 2020, 6:54am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/7 "2020-07-15T06:54:36Z")

</div>

```julia
foo(x::T, y::T) where T = x + 2*y
foo(x, y) = foo(promote(x, y)...)
foo(1, nothing) # would go into an infinite loop without an error

```

> [@schlichtanders](#):
>
> Can `nothing` be converted to `Union{Nothing, Int}` ? yes it can

That’s not how it works (eg everything can trivially be “converted” to a common type `Any`, so by your logic `promote` could just return its arguments and call it a day).

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [July 15, 2020, 7:00am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/8 "2020-07-15T07:00:46Z")

</div>

> [@Tamas\_Papp](#):
>
> ```julia
> foo(x::T, y::T) where T = x + 2*y
> foo(x, y) = foo(promote(x, y)...)
> foo(1, nothing) # would go into an infinite loop without an error
> 
> ```

awesome! understood. Yes, and this seems the common usecase of promote. Still it is not mentioned in the documentation to the best of my understanding. To repeat, the documentation says

> promote(xs…)  
> Convert all arguments to a common type, and return them all (as a tuple). If no arguments can be converted, an error is raised.

The open question is, what is this common type? I would have guessed it is `promote_type(typeof.(xs)...)`. And then, at least in mind, the logic would apply which I sketched.

In this sense I would argue that the documentation is crucially incomplete. `promote` seems to be made for exactly the `foo` use case you described, and hence the common type needs to be a **concrete** type.

---

<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: [July 15, 2020, 7:15am UTC](https://discourse.julialang.org/t/promote-not-working-for-nothing-while-promote-type-works/43028/9 "2020-07-15T07:15:50Z")

</div>

Yes, perhaps that should be clarified. Please consider making a small PR to the docs.
