# Is there a way to remove a certain type from a Union of types?

**URL:** https://discourse.julialang.org/t/is-there-a-way-to-remove-a-certain-type-from-a-union-of-types/42094
**Category:** General Usage
**Tags:** question
**Created:** [June 26, 2020, 10:36am UTC](https://discourse.julialang.org/t/is-there-a-way-to-remove-a-certain-type-from-a-union-of-types/42094 "2020-06-26T10:36:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [June 26, 2020, 10:36am UTC](https://discourse.julialang.org/t/is-there-a-way-to-remove-a-certain-type-from-a-union-of-types/42094/1 "2020-06-26T10:36:10Z")

</div>

Eg. given `Union{Int,Float64,String}` I want to obtain `Union{Int,Float64}` by removing `String`. Is there a way to carry this out?

---

<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: [June 26, 2020, 10:52am UTC](https://discourse.julialang.org/t/is-there-a-way-to-remove-a-certain-type-from-a-union-of-types/42094/2 "2020-06-26T10:52:48Z")

</div>

There is

```julia
julia> Core.Compiler.typesubtract(Union{Int,Float64,String}, String)
Union{Float64, Int64}

```

but as you can see, it is a pretty internal function.

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [December 28, 2020, 3:07pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-remove-a-certain-type-from-a-union-of-types/42094/3 "2020-12-28T15:07:07Z")

</div>

Hi. If there is still interest in the question, this could be an alternative that does not go so deep into internals:

```julia
# Matched types return an empty list of types
excludetype(::Type{T}, ::Type{T}) where T = Type[]

# Return a list of subtypes from (maybe) abstract type
# without the excluded one
function excludetype(abstype, excluded)
    st = subtypes(abstype)
    isempty(st) && return [abstype] # no subtypes
    typelist = Type[]
    for T in st
        if excluded <: T
            append!(typelist, excludetype(T, excluded))
        else
            push!(typelist, T)
        end
    end
    return typelist
end

# Same but for unions
function excludetype(U::Union, excluded)
    return [excludetype(U.a, excluded); excludetype(U.b, excluded)]
end

```

This gives a vector with the filtered list of types that have to be united, and then you can turn it into the filtered `Union`:

```julia
filterfromtype(T, S) = Union{excludetype(T, S)...}

```

For instance:

```julia
julia> filterfromtype(Union{Int,Float64,String}, String)
Union{Float64, Int64}

julia> filterfromtype(Union{Number,String}, Irrational)
Union{AbstractFloat, Integer, String, Complex, Rational}

```

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 17, 2023, 6:48am UTC](https://discourse.julialang.org/t/is-there-a-way-to-remove-a-certain-type-from-a-union-of-types/42094/4 "2023-01-17T06:48:21Z")

</div>

Is there any way to construct a type that includes everything _except_ for a certain type? Something along the lines of,

```julia
T = SetSubtract{Any,MyType}

```

Then, every type would subtype `T` _except_ for `MyType`. I would want this so that `MyType` can indicate something specifically not to dispatch on (in my case, a singleton type).
