# Questions about Union{T,Nothing} and the \`something\` function (like, when do you use it?)

**URL:** <https://discourse.julialang.org/t/questions-about-union-t-nothing-and-the-something-function-like-when-do-you-use-it/19253>\
**Category:** New to Julia\
**Created:** [January 4, 2019, 12:59am UTC](https://discourse.julialang.org/t/questions-about-union-t-nothing-and-the-something-function-like-when-do-you-use-it/19253 "2019-01-04T00:59:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [January 4, 2019, 12:59am UTC](https://discourse.julialang.org/t/questions-about-union-t-nothing-and-the-something-function-like-when-do-you-use-it/19253/1 "2019-01-04T00:59:42Z")

</div>

So, I’ve been reading the docs a lot lately. The FAQ has this crazy thing about nothingness that just raises more questions. I get `nothing`, and I get `Union{T,Nothing} where T` and use it frequently. That’s fine. I have two main things I want to know:

There is this sentence:

> Note that the compiler is able to generate efficient code when working with `Union{T, Nothing}` arguments or fields.

That sounds great. Does that work on arrays? can I have efficient arrays of nullable types? Should I be doing something else if I want to preallocate a large array and fill it later? (can I turn Julia into C? 😛)

* * *

OK, next question. What is this `Some` and `something` stuff? I think I understand the conceptual difference between nothing as a value and nothing as the absence of value (I’ve had to write my private “empty” objects in other languages to mark the absence of a value where I’m handling data structures in a library and the API-user’s nil/null/None is a value, as far as the library is concerned)

I understand that `something` and `Some(nothing)` are somehow related to these usecases, but I really have no idea exactly how or where to use them. Can someone give an example or two?

---

<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:** [January 4, 2019, 1:16am UTC](https://discourse.julialang.org/t/questions-about-union-t-nothing-and-the-something-function-like-when-do-you-use-it/19253/2 "2019-01-04T01:16:23Z")

</div>

> [@ninjaaron](#):
>
> Does that work on arrays? can I have efficient arrays of nullable types?

Yup, definitely! See [Missing Values · The Julia Language](https://docs.julialang.org/en/v1/manual/missing/index.html#Arrays-With-Missing-Values-1) (the docs mention `Union{T, Missing}`, but the same performance results can be obtained for `Union{T, Nothing}`).

> [@ninjaaron](#):
>
> Should I be doing something else if I want to preallocate a large array and fill it later?

Nope, it’s all the same kind of operation:

```julia
julia> x = Array{Union{Int, Nothing}}(undef, 10)
10-element Array{Union{Nothing, Int64},1}:
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing
 nothing

julia> for i in 1:10
         x[i] = i
       end

julia> x
10-element Array{Union{Nothing, Int64},1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

```

> [@ninjaaron](#):
>
> What is this `Some` and `something` stuff? I think I understand the conceptual difference between nothing as a value and nothing as the absence of value

Yup, that’s pretty much exactly the issue that `Some` is designed to solve. You would typically write: “there is a value here and that value is `nothing`” as `Some(nothing)` and you would write “there is no value here” as simply `nothing`.

As for `something`, I think the docs explain it pretty well:

```julia
help?> something
search: something

  something(x, y...)

  Return the first value in the arguments which is not equal to nothing, if any. Otherwise throw an error.
  Arguments of type Some are unwrapped.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> something(nothing, 1)
  1
  
  julia> something(Some(1), nothing)
  1
  
  julia> something(missing, nothing)
  missing
  
  julia> something(nothing, nothing)
  ERROR: ArgumentError: No value arguments present

```

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [January 4, 2019, 3:12pm UTC](https://discourse.julialang.org/t/questions-about-union-t-nothing-and-the-something-function-like-when-do-you-use-it/19253/3 "2019-01-04T15:12:26Z")

</div>

Thanks so much for the response! very helpful!
