# Syntax sugar for Union{T, Void}

**URL:** <https://discourse.julialang.org/t/syntax-sugar-for-union-t-void/826>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [December 9, 2016, 1:40pm UTC](https://discourse.julialang.org/t/syntax-sugar-for-union-t-void/826 "2016-12-09T13:40:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FANG\_Colin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fang_colin/32/170_2.png) [@FANG\_Colin](https://discourse.julialang.org/u/FANG_Colin)\
**Post date:** [December 9, 2016, 1:40pm UTC](https://discourse.julialang.org/t/syntax-sugar-for-union-t-void/826/1 "2016-12-09T13:40:52Z")

</div>

I found myself writing `Union{T, Void}` a lot in the function default arguments, e.g.

```julia
function integers{T}(
        ::Type{T};
        min_value::Union{Integer, Void}=nothing,
        max_value::Union{Integer, Void}=nothing
    )
    min_value::T = min_value == nothing ? typemin(T) : min_value
    max_value::T = max_value == nothing ? typemax(T) : max_value
    @assert min_value <= max_value
    
    rand(min_value:max_value)
end

```

Is there any better way to do it?  
Normally I would make a `typealias Voidable{T} Union{T, Void}`

Would it be useful, if there is a syntax like `T?` is expanding to `Union{T, Void}`?

---

<div class="post-metadata">

**Author:** ![oatlzzvztd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oatlzzvztd/32/3285_2.png) [@oatlzzvztd](https://discourse.julialang.org/u/oatlzzvztd)\
**Post date:** [December 9, 2016, 2:26pm UTC](https://discourse.julialang.org/t/syntax-sugar-for-union-t-void/826/2 "2016-12-09T14:26:27Z")

</div>

There’s the `Nullable{T}` type. I usually alias it to `Maybe{T}`.

---

<div class="post-metadata">

**Author:** ![FANG\_Colin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fang_colin/32/170_2.png) [@FANG\_Colin](https://discourse.julialang.org/u/FANG_Colin)\
**Post date:** [December 9, 2016, 2:39pm UTC](https://discourse.julialang.org/t/syntax-sugar-for-union-t-void/826/3 "2016-12-09T14:39:17Z")

</div>

It is a bit clumsy to ask the caller to pass in `min_value=Nullable(123)` instead of `min_value=123`

`Maybe` sounds a monad to me.

---

<div class="post-metadata">

**Author:** ![joshday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshday/32/368_2.png) [@joshday](https://discourse.julialang.org/u/joshday)\
**Post date:** [December 9, 2016, 2:46pm UTC](https://discourse.julialang.org/t/syntax-sugar-for-union-t-void/826/4 "2016-12-09T14:46:57Z")

</div>

At least in this case, you can just use `typemin(T)` and `typemax(T)` as default arguments:

```julia
function integers{T}(::Type{T}, min_value::T = typemin(T), max_value::T = typemax(T))
    @assert min_value <= max_value
    rand(min_value:max_value)
end

```

---

<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, 2016, 3:38pm UTC](https://discourse.julialang.org/t/syntax-sugar-for-union-t-void/826/5 "2016-12-09T15:38:45Z")

</div>

For specifying optional values, you can always `convert(Nullable, value)` to ensure it is a `Nullable`.

I sometimes wonder about having `get(x)=x` as a fallback. Then I could always use the semantics of `Nullable` for optional arguments, whether they are actually `Nullable` or not. But I am sure I have not thought about all the ramifications.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 9, 2016, 8:28pm UTC](https://discourse.julialang.org/t/syntax-sugar-for-union-t-void/826/6 "2016-12-09T20:28:53Z")

</div>

This isn’t universally applicable, but you can shorten your example to:

```julia
function integers{T}(
        ::Type{T};
    min_value::Integer=typemin(T),
    max_value::Integer=typemax(T)
    )
    @assert min_value <= max_value
    
    rand(min_value:max_value)
end

```
