# Aliases for Union{T, Nothing} and Union{T, Missing}?

**URL:** <https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402>\
**Category:** New to Julia\
**Created:** [September 24, 2018, 12:03am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402 "2018-09-24T00:03:13Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 26, 2018, 11:07am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/21 "2018-09-26T11:07:00Z")

</div>

That’s a good argument. What’s the opposing argument that led to the postponement of a decision? Simply caution?

One idea is then to suggest `T?` for `Union{T, Missing}`, and keep an eye out for something clever for `Nothing`, if it turns out to be useful.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [May 8, 2019, 12:05pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/22 "2019-05-08T12:05:16Z")

</div>

Has there been any progress/further discussion on this? FWIW, I really like `T?` as well.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 8, 2019, 12:07pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/23 "2019-05-08T12:07:30Z")

</div>

> [@carstenbauer](#):
>
> I really like `T?` as well.

Me too. For `Union{T, Nothing}` 😉

Yo used to be able to do

```julia
const ?{T} = Union{T, Nothing}

```

[use nothing instead of Nullable by KristofferC · Pull Request #90 · JuliaWeb/GitHub.jl · GitHub](https://github.com/JuliaWeb/GitHub.jl/pull/90/files#diff-f3f3eb3e7bb7977f66531d9d1f8855a7R2). Beautiful.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 8, 2019, 12:25pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/24 "2019-05-08T12:25:37Z")

</div>

> [@kristoffer.carlsson](#):
>
> For `Union{T, Nothing}`

Without getting into the discussion of data scientists vs software engineers - what use exactly are you needing `T?` for `Union{T, Nothing}` for in actual use?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 8, 2019, 12:36pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/25 "2019-05-08T12:36:08Z")

</div>

> [@mkborregaard](#):
>
> what use exactly are you needing `T?` for `Union{T, Nothing}` for in actual use

`Union{T, Nothing}` is very common for field types and function argument types. Wasting such a nice syntax on a type that most people do not use (`Missing`) would be quite unfortunate in my opinion.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [May 8, 2019, 2:35pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/26 "2019-05-08T14:35:37Z")

</div>

Given that the API of some functions in Julia now gives `nothing` if no sensible output exist, i.e.

```julia
julia> tryparse(Float64, "a")

julia> findfirst(==(3), [1, 2])

```

there was a proposal to create a few `?` based syntactic sugar to work with `nothing` [https://github.com/JuliaLang/julia/issues/26303](https://github.com/JuliaLang/julia/issues/26303).

If that happens, then the `T?` notation would be more consistent for `nothing` than `missing`.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 8, 2019, 3:50pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/27 "2019-05-08T15:50:07Z")

</div>

I explicitly wanted to avoid the discussion of which was most used / common (it depends on what you use Julia for), whether data scientists or software engineers were first class users etc., but instead to discuss what you would use it for. Would you use it in struct definitions?

```julia
struct Mystruct
  x::Int?
end

```

I think that’s not so nice, personally.  
My argument being that it’s mostly used for controlling function dispatch. And that `f(x::Union{T, Nothing}) = ...` is a lot less common than `f(x::Union{T, Missing}) = ...` because in the first case you would often either dispatch on the `T` or `Nothing` or use duck typing.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [May 8, 2019, 5:29pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/28 "2019-05-08T17:29:42Z")

</div>

In some small projects, I’ve defined `|(::Type{A}, ::Type{B}) where {A, B} = Union{A, B}` which always felt really nice. The main annoyance was that you couldn’t do

```julia
struct Foo
    x::Int|Float64
end

```

because of the lowering precedence/greediness, instead, it had to be:

```julia
struct Foo
    x::(Int|Float64)
end

```

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 8, 2019, 5:37pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/29 "2019-05-08T17:37:22Z")

</div>

I also don’t like the `?` because it doesn’t look like a unary operator, especially placed on the end like that.

I like @quinnj’s `Int|Missing` much better, because

- It is already a binary operator.
- It is exlicit enough to handle every case, while still not appearing overly verbose, i.e. it is easy enough to write `Int|Missing` without requiring yet another special alias that is specific to `Missing` and different for `Nothing`.

It also doesn’t bother me personally to need parantheses due to precedence, in fact I prefer it because my brain also parses `x::Int|Float64` as `(x::Int)|Float64`.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 9, 2019, 8:11pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/30 "2019-05-09T20:11:54Z")

</div>

Without being too hyperbolic, I think julia will lose the data science crowd if there’s no convenient syntax for arrays that may contain missing. To be honest I still do some of my data analysis in R because dealing with missings, especially writing functions for them, is so annoying.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 9, 2019, 8:16pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/31 "2019-05-09T20:16:14Z")

</div>

> [@mkborregaard](#):
>
> To be honest I still do some of my data analysis in R because dealing with missings, especially writing functions for them, is so annoying.

Writing `Vector{Int|Missing}` is really that much more annoying that writing `Vector{Int?}`?

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [May 9, 2019, 8:28pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/32 "2019-05-09T20:28:58Z")

</div>

I should probably use missings more, but they are a bit of a hassle. In contrast, NaNs are really easy to handle (and most of my data is Float64 anyway), so I am stuck with that old-fashioned approach.

From my perspective (finance/stats), missings should be as simple as NaNs, that is, `Union{T,Missing}`, should be implicit in `T`.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 9, 2019, 9:09pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/33 "2019-05-09T21:09:14Z")

</div>

> [@Paul\_Soderlind](#):
>
> From my perspective (finance/stats), missings should be as simple as NaNs, that is, `Union{T,Missing}` , should be implicit in `T` .

I get your point, but it’s hard to argue that in the face of a 5x slowdown:

```julia
julia> x = rand(1000);

julia> @btime $x .+ $x;
  437.634 ns (1 allocation: 7.94 KiB)

julia> y = Union{Float64, Missing}[x;];

julia> @btime y .+ y;
  2.164 μs (9 allocations: 8.14 KiB)

```

`NaN`s are implemented in hardware; `missing` is not so lucky…

I’m all for a simplified syntax, btw. Implementing `|` for `Float64|Missing` seems like a great candidate. I also really like using `?` for one of the other, but wouldn’t know which. And did no-one bring up the case `Float64|Missing|Nothing` yet?

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [May 9, 2019, 9:27pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/34 "2019-05-09T21:27:00Z")

</div>

OK, I was probably too greedy.

Still, the handling of missings could be easier. My use case: I load some data that has no missings, do some data wrangling, and then want to set `x[1001,37]=missing`. I believe the current way of doing it is to first do `convert(Array{Union{Float64,Missing}},x)`. Making this simpler (convenience functions? macros?) would help.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 9, 2019, 9:30pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/35 "2019-05-09T21:30:04Z")

</div>

Very good example. Quite similar to

```julia
julia> x = [1:10;];

julia> x[3] = 1.5
ERROR: InexactError: Int64(1.5)

```

I agree with you that a friendlier way of dealing with this would be very nice.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 9, 2019, 9:42pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/36 "2019-05-09T21:42:03Z")

</div>

You have `allowmissing` function in Missings.jl.

Additionally, while I personally like `Int|Missing` syntax my experience with casual data scientists is that they would prefer something shorter. Exactly for this reasons in DataFrames.jl we use `⍰` when printing column type when displaying a data frame as otherwise it was simply too long.

If `T?` is taken for `Union{T, Nothing}` (I get the arguments behind it) then maybe we could choose some other short signal of missingness, e.g. a double `??` (it is not ideal though) or `|?`?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 9, 2019, 9:43pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/37 "2019-05-09T21:43:13Z")

</div>

eltype forcing assignment?

```julia
x = [1, 2, 3];
typeof(x) == Array{Int64, 1}
true

x[2] := 2.0;
typeof(x) == Array{Float64, 1}
true

```

seems highly energetic and frothy but not in a good way

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [May 9, 2019, 10:06pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/38 "2019-05-09T22:06:32Z")

</div>

> [@tkluck](#):
>
> I agree with you that a friendlier way of dealing with this would be very nice.

Code that needs to do this kind of stuff in e.g. `Tables.buildcolumns` ends up defining things along the lines of `setindex_or_widen!` or `push_or_widen!` that checks the eltype and if it’s compatible does the operation in place and return the first argument, otherwise expands the type and returns a different object. Maybe there could be some macro that makes this less clumsy? Say some

```julia
@widen push!(x, a)
@widen x[i] = a

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 9, 2019, 10:28pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/39 "2019-05-09T22:28:39Z")

</div>

Personally, I feel that `T?` meaning `Union{T, Missing}` or `Union{T, Nothing}` would decrease the legibility and approachability of code. I like the `T|Missing` solution quite a bit but for people who complain that’s still too much writing, I think we should just allow `?` to be used in variable / constant names such that one is free to define

```julia
const Int? = Int|Missing

```

or

```julia
const Int? = Int|Nothing

```

if that’s what they really need, but it seems like a bad thing to provide by default from base.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 9, 2019, 10:38pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/40 "2019-05-09T22:38:42Z")

</div>

I would like `'\questiondown'` ( **¿** ) and `'\exclaimdown'` ( **¡** ) to become available within Julia.

[Previous page](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402.md?page=1)

[Next page](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402.md?page=3)
