# Strange promote\_rule()?

**URL:** <https://discourse.julialang.org/t/strange-promote-rule/25762>\
**Category:** New to Julia\
**Created:** [June 27, 2019, 11:47am UTC](https://discourse.julialang.org/t/strange-promote-rule/25762 "2019-06-27T11:47:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [June 27, 2019, 11:47am UTC](https://discourse.julialang.org/t/strange-promote-rule/25762/1 "2019-06-27T11:47:51Z")

</div>

compared to `promote_type()`, `promote_rule()` seems to be asymmetric and wrong?

```julia
julia> promote_type(Float16, Float32)
Float32

julia> promote_type(Float32, Float16)
Float32

julia> promote_rule(Float16, Float32) # why???
Union{}

julia> promote_rule(Float32, Float16)
Float32

julia> promote_rule(Float16, Float16) # why???
Union{}

```

is it a bug?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 27, 2019, 12:17pm UTC](https://discourse.julialang.org/t/strange-promote-rule/25762/2 "2019-06-27T12:17:00Z")

</div>

> [@tomtom](#):
>
> is it a bug?

No, you can read about it here: [Conversion and Promotion · The Julia Language](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Defining-Promotion-Rules-1). In particular:

> Also note that one does not need to define both `promote_rule(::Type{A}, ::Type{B})` and `promote_rule(::Type{B}, ::Type{A})` – the symmetry is implied by the way `promote_rule` is used in the promotion process.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [June 27, 2019, 12:19pm UTC](https://discourse.julialang.org/t/strange-promote-rule/25762/3 "2019-06-27T12:19:28Z")

</div>

In short, you define `promote_rule` but you use `promote_type`.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [June 27, 2019, 1:07pm UTC](https://discourse.julialang.org/t/strange-promote-rule/25762/4 "2019-06-27T13:07:06Z")

</div>

> [@fredrikekre](#):
>
> the symmetry is implied by the way `promote_rule` is used in the promotion process.

what does it mean???

does it mean that

```julia
julia> promote_rule(Float32, Float16)
Float32

```

is well defined.

but,

```julia
julia> promote_rule(Float16, Float32) # why???
Union{}

```

is NOT defined, so a strange `Union{}` is returned???

---

<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:** [June 27, 2019, 2:47pm UTC](https://discourse.julialang.org/t/strange-promote-rule/25762/5 "2019-06-27T14:47:39Z")

</div>

You don’t want to use `promote_rule` here. Instead use `promote_type`.

The key is like Stefan said, types _define_ `promote_rule` for one of the argument orders, and then you _call_ `promote_type` (or simply `promote`) to get the type (or values) that result from the promotion. When Julia tries to figure out what a given promotion should be (via `promote_type` or `promote`) it tries `promote_rule` _both ways_ for you and unions the result.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 27, 2019, 2:51pm UTC](https://discourse.julialang.org/t/strange-promote-rule/25762/6 "2019-06-27T14:51:50Z")

</div>

you don’t have to be aggressive. You can reason `rule` returning `Union{}` because

```julia
julia> Union{Float32, Union{}}
Float32

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [July 1, 2019, 11:00am UTC](https://discourse.julialang.org/t/strange-promote-rule/25762/7 "2019-07-01T11:00:46Z")

</div>

I was not aggressive … English is not my first language… may not express good enough 🙏
