# How to get the non-Nothing type from \`Union{T,Nothing}\`

**URL:** https://discourse.julialang.org/t/how-to-get-the-non-nothing-type-from-union-t-nothing/30523
**Category:** General Usage
**Tags:** question
**Created:** [October 31, 2019, 7:03am UTC](https://discourse.julialang.org/t/how-to-get-the-non-nothing-type-from-union-t-nothing/30523 "2019-10-31T07:03:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [October 31, 2019, 7:03am UTC](https://discourse.julialang.org/t/how-to-get-the-non-nothing-type-from-union-t-nothing/30523/1 "2019-10-31T07:03:26Z")

</div>

If I define

```julia
const Maybe{T} = Union{T,Nothing}

```

How to “pop” the `T` type from `Maybe`? I want to do this because I have the following type

```julia
using Parameters

@with_kw struct A
    a::Maybe{String} = nothing
    b::Maybe{Float64} = nothing
    c::Maybe{Int} = nothing
    d::Int = 1
end

```

I want to parse this type from matched strings by

```julia
[parse(fieldtype(A, f), match(regex, str)[1]) for (f, regex) in zip(fieldnames(A), REGEXES)]

```

where each field `f` is associated with a `regex`.

Of course, I can use `setdiff` to kick `Nothing` out of that `Maybe`. But the code looks ugly…

Another problem is that I cannot `parse` a `String` from a `String`, but that is not a big deal.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 31, 2019, 7:13am UTC](https://discourse.julialang.org/t/how-to-get-the-non-nothing-type-from-union-t-nothing/30523/2 "2019-10-31T07:13:02Z")

</div>

There is ‘nonmissingtype’ that does this for ’Missing’. You could look at it’s definition and do the same for ‘Nothing’.

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [October 31, 2019, 7:23am UTC](https://discourse.julialang.org/t/how-to-get-the-non-nothing-type-from-union-t-nothing/30523/3 "2019-10-31T07:23:18Z")

</div>

Hmmm. It is the first time I heard about this function. It uses `Core.Compiler.typesubtract`. Thank you.

A poor man’s `nonnothingtype`:

```julia
nonnothingtype(::Type{T}) where {T} = Core.Compiler.typesubtract(T, Nothing)

```

I did a little bit search and found some links, hope this would help others:

1. [Get non-missing type in the case of parametric type](https://discourse.julialang.org/t/get-non-missing-type-in-the-case-of-parametric-type/29109)
2. [Base.nonmissingtype and Core.Compiler.typesubtract aren't exported](https://discourse.julialang.org/t/base-nonmissingtype-and-core-compiler-typesubtract-arent-exported/24367)
3. [Dispatch using Union and order of arguments](https://discourse.julialang.org/t/dispatch-using-union-and-order-of-arguments/19732)
