# Union{Nothing, T} vs Union{Nothing, Some{T}) -- I don't understand the utility of Some(nothing)

**URL:** https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639
**Category:** General Usage
**Created:** [August 17, 2018, 9:51pm UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639 "2018-08-17T21:51:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)
#### Post date: [August 17, 2018, 9:51pm UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639/1 "2018-08-17T21:51:29Z")

</div>

I’m trying to figure out what to do with my code that uses `Nullable`. After looking at the documentation, some posts on here, and [this blog post](https://julialang.org/blog/2018/06/missing) I don’t really understand when to use `Union{Nothing, Some{T}}` over `Union{Nothing, T}`. The blog post says:

> As a special case, if `nothing` is a possible value (i.e. `Nothing <: T` ), `Union{Nothing,Some{T}}` should be used instead

which still doesn’t make sense to me. When would it be useful to distinguish `nothing` from `Some(nothing)`?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [August 17, 2018, 10:19pm UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639/2 "2018-08-17T22:19:51Z")

</div>

It’s a matter of how your code interprets the data. For example, `nothing` could mean, “this attribute is not stored for this object” while `Some(nothing)` could mean, “this attribute is stored, and its value is `nothing`”. Without the `Some()` wrapper, you can’t distinguish those two cases.

Or another way of putting it: you might ask me what kind of car I own. I might not respond (`return nothing`) or I might respond that I do not own a car (`return Some(nothing)`).

---

<div class="post-metadata">

### Author: ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)
#### Post date: [August 17, 2018, 10:35pm UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639/3 "2018-08-17T22:35:31Z")

</div>

Love it, very clear, thanks!

---

<div class="post-metadata">

### Author: ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)
#### Post date: [August 18, 2018, 3:32am UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639/4 "2018-08-18T03:32:20Z")

</div>

Another example would be a dictionary object, where you look up a particular key. If the key is not found you might return `nothing`, but the key might exist with a value of `nothing`. So to distinguish those two cases you might return `Some(v)` for the value of any key (it could be `Some(nothing)`, and `nothing` when the key isn’t found.

---

<div class="post-metadata">

### Author: ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)
#### Post date: [August 18, 2018, 4:27pm UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639/5 "2018-08-18T16:27:08Z")

</div>

How does this relate to the `nothing` vs. `missing` distinction?

---

<div class="post-metadata">

### Author: ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)
#### Post date: [August 18, 2018, 6:07pm UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639/6 "2018-08-18T18:07:55Z")

</div>

I think `missing` is designed to silently propagate, as a data value that exists and is unknown, while `nothing` is designed to be the absence of a data value, and not silently propagate. I think the intent is for `missing` to be accepted by most functions that take normal values, while `nothing` is not.

Also `nothing` and `missing` behave quite differently in comparisons, e.g.

```julia
julia> 1 == nothing
false

julia> 1 == missing
missing

julia> nothing == nothing
true

julia> missing == missing
missing

julia> 1 + missing
missing

julia> 1 + nothing
ERROR: MethodError: no method matching +(::Int64, ::Nothing)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
  +(::T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, ::T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:53
  +(::Union{Int16, Int32, Int64, Int8}, ::BigInt) at gmp.jl:456
  ...
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

### Author: ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)
#### Post date: [August 20, 2018, 10:34am UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639/7 "2018-08-20T10:34:01Z")

</div>

Thanks for the detailed reply. I’m still confused as to why one would use `Some(nothing)` instead of `missing` (if that even makes sense…)

In particular, I’m trying to figure out to what extent the similarities in the following two quotes indeed correspond to equivalent concepts:

> For example, `nothing` could mean, “this attribute is not stored for this object” while `Some(nothing)` could mean, “this attribute is stored, and its value is nothing”.

> I think `missing` is designed to [be] a data value that exists and is unknown, while `nothing` is designed to be the absence of a data value

Reading the two, it seems to me that `missing` is intended to play a similar role than `Some(nothing)`, so when should one use one or the other?

---

<div class="post-metadata">

### Author: ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)
#### Post date: [August 20, 2018, 1:16pm UTC](https://discourse.julialang.org/t/union-nothing-t-vs-union-nothing-some-t-i-dont-understand-the-utility-of-some-nothing/13639/8 "2018-08-20T13:16:11Z")

</div>

`missing` means “there is a valid value, but we don’t know what it is”. This is most often used in a data analysis context.

In the examples above `Some(nothing)` is used to say "there is a value here, we know what it is, and the value is `nothing`. In this case `nothing` has some meaning that depends on the context. For instance, say you have a function that wraps another function and returns its return value on success, but on failure returns `nothing`. The problem is that the wrapped function might itself return `nothing`, so you wrap the return value in `Some(...)`. So in this case `Some(nothing)` would mean that the wrapped function returned `nothing`, and `nothing` means that an error occurred.

(note this specific example is a bad idea in most cases, because you lose all information about the error that occured)

code example:

```julia
function catcherr(f)
    try
        Some(f())
    catch
        nothing
    end
end

foo() = 42
bar() = nothing
baz() = raise(ErrorException("badness"))

julia> catcherr(foo)
Some(42)

julia> catcherr(bar)
Some(nothing)

julia> catcherr(baz) # returns `nothing`

julia>

```
