# Get non-missing type in the case of parametric type

**URL:** https://discourse.julialang.org/t/get-non-missing-type-in-the-case-of-parametric-type/29109
**Category:** General Usage
**Tags:** parametric-types
**Created:** [September 24, 2019, 1:05pm UTC](https://discourse.julialang.org/t/get-non-missing-type-in-the-case-of-parametric-type/29109 "2019-09-24T13:05:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [September 24, 2019, 1:05pm UTC](https://discourse.julialang.org/t/get-non-missing-type-in-the-case-of-parametric-type/29109/1 "2019-09-24T13:05:44Z")

</div>

So I was looking at how to get `T` from a `Union{Missing,T}` and came up with

```julia
nonmissing(TT::Type{Union{Missing,T}}) where T = T
nonmissing(TT::Type{T}) where T = T

```

(PS: incidentally I found that @bkamins had answered something similar on stackoverflow [Is there a type subtraction operation in Julia? - Stack Overflow](https://stackoverflow.com/questions/52118361/is-there-a-type-subtraction-operation-in-julia) so it looks like this is not a bad idea)

This works fine for non-parametric types including abstract types:

```julia
julia> nonmissing(Union{Missing,Real}) == Real

```

However when there’s a parametric type, it returns the union

```julia
julia> struct Foo{N} end
julia> nonmissing(Union{Missing,Foo}) == Union{Missing,Foo}

```

Is there a way to recuperate `Foo` in this case? One way I found around it that works is to write a macro but I feel that’s overkill here…

```julia
macro nonmissing(ex)
  ex isa Symbol && return ex
  types = ex.args[2:end]
  types[1] == :Missing && return types[2]
  return types[1]
end

```

thanks

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [September 24, 2019, 1:40pm UTC](https://discourse.julialang.org/t/get-non-missing-type-in-the-case-of-parametric-type/29109/2 "2019-09-24T13:40:11Z")

</div>

Hmm I guess this does the job:

```julia
nomiss(::Type{T}) where T = T isa Union ? ifelse(T.a == Missing, T.b, T.a) : T

```

but I’ll take anything that’s more elegant than that or the macro 🙂

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [September 24, 2019, 1:43pm UTC](https://discourse.julialang.org/t/get-non-missing-type-in-the-case-of-parametric-type/29109/3 "2019-09-24T13:43:51Z")

</div>

Well now (SO answer is old 🙂 and things move forward) you have `nonmissingtype` defined as:

```
nonmissingtype(::Type{S}) where {S} = Core.Compiler.typesubtract(S, Missing)

```

and it does the job as expected (please correct me if I missed something).

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [September 24, 2019, 1:47pm UTC](https://discourse.julialang.org/t/get-non-missing-type-in-the-case-of-parametric-type/29109/4 "2019-09-24T13:47:15Z")

</div>

Nice, thanks, unfortunately that won’t work on older versions of Julia right? it doesn’t on 1.0. as far as I can tell. (for `Union{Missing,Foo}` it will return `Union{Missing,Foo}`)

I guess I could do a version check and either use that or the `nomiss` thing.

Thanks either way!

---

<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 24, 2019, 3:04pm UTC](https://discourse.julialang.org/t/get-non-missing-type-in-the-case-of-parametric-type/29109/5 "2019-09-24T15:04:14Z")

</div>

You can use Missings.jl, which exports `nonmissingtype` on older Julia versions.
