# Missing or NaN

**URL:** <https://discourse.julialang.org/t/missing-or-nan/12729>\
**Category:** General Usage\
**Created:** [July 30, 2018, 12:19am UTC](https://discourse.julialang.org/t/missing-or-nan/12729 "2018-07-30T00:19:37Z")\
**Posts on this page:** 7\
**Page:** 2

<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:** [July 31, 2018, 8:55am UTC](https://discourse.julialang.org/t/missing-or-nan/12729/21 "2018-07-31T08:55:18Z")

</div>

> [@oheil](#):
>
> they know about NA, but there is no awareness about the special issues and differences which comes with it

What you describe as “no awareness” may, at least partially, just be a consequence of an overly complex and _ad hoc_ design.

I taught a PhD-level course using R for years (before switching to Julia), and used it for about 15 years, but I am certain that there are corner cases of `NA`/`NaN` in R that would surprise me, and I would even find it difficult to locate them in the documentation _ex post_. Eg `?NA` says

> Numerical computations using ‘NA’ will normally result in ‘NA’

but a frequently discussed exception is

```R
> NA^0
[1] 1

```

for which it is possible to concoct rationalizations, but I find it unexpected. Cf

```julia
julia> missing^0
missing

```

which is very clean semantics.

---

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [July 31, 2018, 7:56pm UTC](https://discourse.julialang.org/t/missing-or-nan/12729/22 "2018-07-31T19:56:54Z")

</div>

I don’t understand how you use this. When is T filled in with a concrete type?

- Lewis

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 31, 2018, 8:09pm UTC](https://discourse.julialang.org/t/missing-or-nan/12729/23 "2018-07-31T20:09:12Z")

</div>

You don’t have to filll it in with a concrete type, you could define:

```julia
using Missings
const M{T} = Union{Missing, T} where T <: Any
x = [1,2,3, missing]
eltype(x) == M{Int64}
> true

```

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [July 31, 2018, 9:32pm UTC](https://discourse.julialang.org/t/missing-or-nan/12729/24 "2018-07-31T21:32:46Z")

</div>

Maybe worth noting, that in v0.7/1.0 the Missings package is no longer needed. Missing support is built in. Example:

```julia
# Version 0.7.0-beta2.191 (2018-07-30 22:39 UTC)
julia> x = [1, 2, 3, missing]
4-element Array{Union{Missing, Int64},1}:
 1
 2
 3
  missing

julia> eltype(x)
Union{Missing, Int64}

julia> eltype.(x)
4-element Array{DataType,1}:
 Int64
 Int64
 Int64
 Any

```

Union{Missing,T} would be needed (here) to specify a non-standard element type (e.g. Int32 instead of Int64), and, maybe, in this case an alias like M{T} could be helpful:

```julia
julia> y = Array{Union{Int32, Missing}}([1, 2, 3, missing])
4-element Array{Union{Missing, Int32},1}:
 1
 2
 3
  missing

julia> M{T} = Union{Missing, T} where T
Union{Missing, T} where T

julia> z = Array{M{Int32}}([1, 2, 3, missing])
4-element Array{Union{Missing, Int32},1}:
 1
 2
 3
  missing

```

Not sure I would bother with M{T} though as it is not as obvious as Union{Missing, T}. Maybe I’m wrong, but with a `T?` syntax I could image that one could write: `Array{Int32?}([1, 2, 3, missing])` in a future v1.x version?

---

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [August 1, 2018, 3:50pm UTC](https://discourse.julialang.org/t/missing-or-nan/12729/25 "2018-08-01T15:50:25Z")

</div>

Thanks.

I am sure I don’t understand metaprogramming with Julia.

For example, the \<: Any seems unnecessary. By not supplying a concrete or abstract type for T, T can stand for any type. Leaving out \<: Any, I get the same type alias you created.

**julia\>** using Missings

**julia\>** const M{T} = Union{Missing, T} where T

Union{Missings.Missing, T} where T

Then, I also don’t understand what to do with this type. I can’t use it in an array constructor, for example:

**julia\>** using Missings

**julia\>** const M{T} = Union{Missing, T} where T \<: Any

Union{Missings.Missing, T} where T

**julia\>** b = Array{M{T},1}([1,2,3,missing])

\*_ERROR:_\* UndefVarError: T not defined

I note that in making a comparison to the type alias, I have to fill in T. it’s beginning to sink in, maybe, that a type declaration that references a type variable (T), is a little like a function in usage (but, otherwise not at all). The type variable is a parameter that has to be filled in. See at the end…

**julia\>** using Missings

**julia\>** const M{T} = Union{Missing, T} where T \<: Any

Union{Missings.Missing, T} where T

**julia\>** b = Array{M{T},1}([1,2,3,missing])

\*_ERROR:_\* UndefVarError: T not defined

**julia\>** b = [1,2,3,missing]

4-element Array{Union{Int64, Missings.Missing},1}:

1

2

3

missing

**julia\>** typeof(b)

Array{Union{Int64, Missings.Missing},1}

**julia\>** eltype(b)

Union{Int64, Missings.Missing}

But, I can do this using the type alias when I fill in T with a concrete type:

**julia\>** c = Array{M{Int64},1}([1,2,3, missing])

4-element Array{Union{Int64, Missings.Missing},1}:

1

2

3

missing

**julia\>** d = Array{M{String},1}()

0-element Array{Union{Missings.Missing, String},1}

**julia\>** push!(d, “foo”)

1-element Array{Union{Missings.Missing, String},1}:

“foo”

**julia\>** push!(d, missing)

2-element Array{Union{Missings.Missing, String},1}:

“foo”

missing

I see this is the building block for a single type alias that can support all of the needed union types that a user could ever want. That simplifies things.

Still, I can’t escape the feeling that Julia is increasingly painting itself into the corner of a theoretical “demonstrator” language, a sort of ml for ML if you catch my drift. That’s certainly not the intent, and Julia works for pretty much all of what I previously did in Python, and with a lot more satisfaction.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 1, 2018, 4:22pm UTC](https://discourse.julialang.org/t/missing-or-nan/12729/26 "2018-08-01T16:22:49Z")

</div>

In the future, you can ensure your code is more readable by quoting it with three backticks

````julia
``` 
julia> your code can go here
```

````

To clarify, this is not about metaprogramming. Metaprogramming is the process of inspecting expressions at compile time and altering them to change their behavior. Because types are first-class citizens in Julia, you can create aliases for them easily, but this is different than metaprogramming.

I add the `where T <: Any` at the end just because it makes the whole line more readable. Notice that `T` is essentially a variable that is defined for the scope of the expression where I define `M{T}`. `T` is defined as simply being any type, but outside of that code it has no meaning.

---

<div class="post-metadata">

**Author:** ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)\
**Post date:** [August 1, 2018, 4:33pm UTC](https://discourse.julialang.org/t/missing-or-nan/12729/27 "2018-08-01T16:33:15Z")

</div>

I use backticking in the discourse editor.

Let’s see if it works in email:

```julia

Sample code

```

Thanks for clarification on metaprogramming. To further clarify, the type variable needs to be filled in with a type expression when an actual type is being instantiated.

[Previous page](https://discourse.julialang.org/t/missing-or-nan/12729.md?page=1)
