# All Types Except One

**URL:** <https://discourse.julialang.org/t/all-types-except-one/116611>\
**Category:** General Usage\
**Tags:** type\
**Created:** [July 4, 2024, 3:12am UTC](https://discourse.julialang.org/t/all-types-except-one/116611 "2024-07-04T03:12:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [July 4, 2024, 3:12am UTC](https://discourse.julialang.org/t/all-types-except-one/116611/1 "2024-07-04T03:12:43Z")

</div>

Is there any way to simply that I want a type that includes everything but one? I’d expect a syntax like

`Union{Number \ Bool, String}`

I saw the answer below, but it seems that the function doesn’t exist anymore

> [@Is there a way to remove a certain type from a Union of types?](https://discourse.julialang.org/t/is-there-a-way-to-remove-a-certain-type-from-a-union-of-types/42094):
>
> 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:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 4, 2024, 4:21am UTC](https://discourse.julialang.org/t/all-types-except-one/116611/2 "2024-07-04T04:21:15Z")

</div>

no. that said you can make a function that matches these inputs by defining 1 method for Union{Number, String} and a separate method for Bool

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [July 4, 2024, 6:13am UTC](https://discourse.julialang.org/t/all-types-except-one/116611/3 "2024-07-04T06:13:06Z")

</div>

Is there any reason for not having this? In the sense of defining types with  
a set difference being discouraged?

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [July 4, 2024, 10:00am UTC](https://discourse.julialang.org/t/all-types-except-one/116611/4 "2024-07-04T10:00:39Z")

</div>

See also [this](https://discourse.julialang.org/t/bools-in-arithmetic-operations/52504/4) thread, where I wanted to exclude `Bool` inputs from `Integer`:

> const ExceptBool = Union{Signed, Unsigned}

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [July 4, 2024, 10:08am UTC](https://discourse.julialang.org/t/all-types-except-one/116611/5 "2024-07-04T10:08:26Z")

</div>

Or in the general case, where this approach might be overly wordy you could also define a more specific method for the excluded type (although you don’t get a `MethodError` anymore)

```julia
foo(::Integer) = bar()
foo(::Bool) = error()

```

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [July 4, 2024, 10:15am UTC](https://discourse.julialang.org/t/all-types-except-one/116611/6 "2024-07-04T10:15:04Z")

</div>

Yes, it could be wordy in the general case, but it is needed only once, e.g. in `startup.jl`, and then `ExceptBool` or similar can be used for all relevant methods.

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [July 4, 2024, 10:18am UTC](https://discourse.julialang.org/t/all-types-except-one/116611/7 "2024-07-04T10:18:39Z")

</div>

Sure, often there are many ways, each with its own distinct set of advantages and limitations.
