# 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:** 1

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [September 24, 2018, 12:03am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/1 "2018-09-24T00:03:14Z")

</div>

Does Julia (v1.0) define or is there a convention for aliases for `Union{T, Nothing}` and `Union{T, Missing}`?  
For example

```julia
const Maybe{T} = Union{T,Nothing}
const Option{T} = Union{T, Missing}

```

I am aware of wrapper type Some{T}, but that is used for the value `nothing` as opposed to `Union{T,Nothing}` which is used for values that are absent.

---

<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:** [September 24, 2018, 1:52am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/2 "2018-09-24T01:52:06Z")

</div>

There are no such aliases predefined.  
`Missing` refers to a data element that may not be available, rather than an option.  
`const Maybe{T} = Union{T, Nothing}` (or similar) is used in Haskell

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [September 24, 2018, 1:54am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/3 "2018-09-24T01:54:17Z")

</div>

What would be a more suitable alias for `Union{T, Missing}`?

---

<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:** [September 24, 2018, 2:03am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/4 "2018-09-24T02:03:17Z")

</div>

```julia
const Maybe{T} = Union{T, Nothing}
const MaybeData{T} = Union{T, Missing}

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [September 24, 2018, 2:10am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/5 "2018-09-24T02:10:26Z")

</div>

@JeffreySarnoff  
So now we have

```julia
Maybe{T} # value present or absent
Some{T} # always a value including the value nothing
MaybeData{T} # value present or missing
DataValue{T} # ?

```

The Julia manual has the section [[Frequently Asked Questions · The Julia Language](https://docs.julialang.org/en/v1/manual/faq/#Nothingness-and-missing-values-1)]. There it says “To represent missing data in the statistical sense (NA in R or NULL in SQL), use the missing object.” QueryVerse uses DataValue{T} for this.  
What is the semantic difference between MaybeData{T} and DataValue{T} ?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 24, 2018, 2:33am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/6 "2018-09-24T02:33:14Z")

</div>

I thought there was a proposal to parse `T?` as `Union{T,Missing}`, but I can’t find it.

---

<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:** [September 24, 2018, 2:35am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/7 "2018-09-24T02:35:23Z")

</div>

No. I don’t what `DataValue{T}` is in the context of your question. That was added into QueryVerse long before the Missing and Nothing state-of-affairs had been settled. While I have no doubt it is useful for David … it is not used outside of that as far as I know.

I was offering my opinion on how to capture the semantics of “the value of that cell or that row or that column is either present or it is missing” in a compact way that is similar to the use/construction of `Maybe{T}`.

---

<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:** [September 24, 2018, 2:48am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/8 "2018-09-24T02:48:57Z")

</div>

I recall that.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [September 24, 2018, 2:50am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/9 "2018-09-24T02:50:14Z")

</div>

Sorry for nitpicking. Going from Julia v1.0 forward, is there a need to go beyond the following?

```julia
Maybe{T} # value present or absent
Some{T} # always a value including the value nothing
MaybeData{T} # value present or missing

```

---

<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:** [September 24, 2018, 2:52am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/10 "2018-09-24T02:52:00Z")

</div>

Julia 2.0 reserves the right to go beyond any present  
(until then, you should be good with those three)  
😉

---

<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:** [September 24, 2018, 5:53am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/11 "2018-09-24T05:53:13Z")

</div>

> [@JeffreySarnoff](#):
>
> (until then, you should be good with those three)

I think that _extensions_ don’t break 1.0, so in theory they would be OK.

That said, I prefer `Union{T, Missing}` & similar. I am sure I would have a hard time remembering which of `Maybe`, `Option`, and other variants corresponds to which singleton (as the discussion demonstrates, this is not intuitive at all); just as I don’t remember precedence tables so I try to avoid relying on them.

---

<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:** [September 24, 2018, 6:04am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/12 "2018-09-24T06:04:39Z")

</div>

I was responding to the question, not advocating syntax.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [September 25, 2018, 8:10pm UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/13 "2018-09-25T20:10:30Z")

</div>

> [@stevengj](#):
>
> I thought there was a proposal to parse `T?` as `Union{T,Missing}` , but I can’t find it.

It’s been discussed a lot of times, but people do not always agree as to whether it should be reserved for `Union{T,Missing}` or `Union{T,Nothing}`.

Until then I wouldn’t recommend using custom aliases as it would make code inconsistent from one project to another.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [September 26, 2018, 2:46am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/14 "2018-09-26T02:46:26Z")

</div>

Despite my original question, I actually agree with this. Clarity is more important than number of characters.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [September 26, 2018, 7:42am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/15 "2018-09-26T07:42:54Z")

</div>

Sometimes less characters does lead to clarity though.  
I wouldn’t advocate it all the time,  
but I think that

```julia
const Opt{T} = Union{Missing, T}
struct Metadata
    shortname::Opt{String}
    fullname::Opt{String}
    website::Opt{String}
    description::Opt{String}
    author::Opt{Vector{String}}
    maintainer::Opt{String}
    license::Opt{String}
    published_date::Opt{Union{Date, DateTime, String}}
    create_date::Opt{Union{Date, DateTime, String}}
    modified_date::Opt{Union{Date, DateTime, String}}
    paper_cite::Opt{String}
    dataset_cite::Opt{String}
    dataurls::Opt{Vector}
    datachecksums::Any
end

```

* * *

is clear than

* * *

```julia
struct Metadata
    shortname::Union{Missing, String}
    fullname::Union{Missing, String}
    website::Union{Missing, String}
    description::Union{Missing, String}
    author::Union{Missing, Vector{String}}
    maintainer::Union{Missing, String}
    license::Union{Missing, String}
    published_date::Union{Missing, Date, DateTime,String}
    create_date::Union{Missing, Date, DateTime, String}
    modified_date::Union{Missing, Date, DateTime, String}
    paper_cite::Union{Missing, String}
    dataset_cite::Union{Missing, String}
    dataurls::Union{Missing, Vector}
    datachecksums::Any
end

```

Because it is just less verbose, so it is easier (for me at least) too see what is going on.

However, such sortage structs are not a particularly common use case.

(Though this type has **a number of other issues** , that will cause it to be not nesc great for performance; those are irrelevant in this question)

---

<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, 10:11am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/16 "2018-09-26T10:11:30Z")

</div>

> [@nalimilan](#):
>
> It’s been discussed a lot of times, but people do not always agree as to whether it should be reserved for `Union{T,Missing}` or `Union{T,Nothing}` .

I think it would be nice to have one for either, maybe `T?` and `T??`, or something like that. There should be a good mnemonic, though, so they wouldn’t be too easy to confuse (`^T`, `?T`) ???

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [September 26, 2018, 10:51am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/17 "2018-09-26T10:51:50Z")

</div>

Crazy idea:  
Infix notation for `Union`.

```julia
julia> ∪ᵀ(x...) = Union{x...}
∪ᵀ (generic function with 1 method)

julia> Int ∪ᵀ Nothing
Union{Nothing, Int64}

julia> Int ∪ᵀ Missing
Union{Missing, Int64}

```

`∪ᵀ` is just a placeholder.

---

<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, 10:58am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/18 "2018-09-26T10:58:55Z")

</div>

Hmm. Maybe, it’s just that `T?` seems _so nice_, and terse. Too bad we need two of them.

---

<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:** [September 26, 2018, 11:00am UTC](https://discourse.julialang.org/t/aliases-for-union-t-nothing-and-union-t-missing/15402/19 "2018-09-26T11:00:39Z")

</div>

`T?` is great. And it should _really_ be for `Union{Missing, T}`.

---

<div class="post-metadata">

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

</div>

In general,  
a short hand for `Union{T, Nothing}`  
does not seem that important

Since one is not supposed to propagate `nothing`s,  
thus making `Union{T,Nothing}` function type constraint,  
a struct field type, or a type parameters should be much rarer than  
the case of `Union{T, Missing}`

Generally `Union{T,Nothing}` is most common only as a return type hint,  
I would think.  
And return type hints are rarely required, since the return type is  
automatically determined.

I expect anything that sometimes returns `nothing` to have that `nothing` dealt with close to the source,  
not passed around for ages.

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