# Set disjunction for arguments, if defining a function

**URL:** <https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124>\
**Category:** General Usage\
**Created:** [November 15, 2019, 11:19am UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124 "2019-11-15T11:19:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [November 15, 2019, 11:19am UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/1 "2019-11-15T11:19:39Z")

</div>

If you define a function, can you set disjunctions for the arguments? For example;

```julia
function simpleadd(a::Float64||Int64, b::Float64||Int64)
       a + b
end

```

I could use `Any`, but this alternative would be more expressive and definitive.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [November 15, 2019, 11:28am UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/2 "2019-11-15T11:28:23Z")

</div>

Are you looking for `Union{Float64,Int64}`? A good overview can be found [here](https://docs.julialang.org/en/v1/manual/types/#Type-Unions-1).

---

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [November 15, 2019, 1:07pm UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/3 "2019-11-15T13:07:06Z")

</div>

Thanks.

Not exactly looking for that, but really for what’s in the example.

It still works, of course, and so far comes closest to what I was looking for. It will do.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 15, 2019, 3:52pm UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/4 "2019-11-15T15:52:44Z")

</div>

> [@Usor](#):
>
> Not exactly looking for that, but really for what’s in the example.

Can you explain what you’re actually looking for? Do you just want an infix operator to express `Union`, or are you actually looking for different semantics?

---

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [November 15, 2019, 5:20pm UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/5 "2019-11-15T17:20:34Z")

</div>

Pretty much like it’s in the example: you use the existing disjunction operator `||` to signify a disjunction for the argument type. The `Union` way proposed works and is equivalent results-wise.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 15, 2019, 7:15pm UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/6 "2019-11-15T19:15:12Z")

</div>

It’s possible to do this with the `|` operator:

```julia
julia> Base.:|(::Type{T}, ::Type{S}) where {T,S} = Union{T,S}

julia> simpleadd(a::(Float64|Int64), b::(Float64|Int64)) = a + b
simpleadd (generic function with 1 method)

julia> simpleadd(3, 4.2)
7.2

julia> methods(simpleadd)
# 1 method for generic function "simpleadd":
[1] simpleadd(a::Union{Float64, Int64}, b::Union{Float64, Int64})

```

I wouldn’t recommend it though — defining an idiosyncratic syntax for `Union` will make your code harder for other Julia programmers to read, not to mention the fact that this is [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/index.html#Avoid-type-piracy-1) and hence not composable (if someone else overloads `|` for types).

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [November 15, 2019, 8:31pm UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/7 "2019-11-15T20:31:25Z")

</div>

You could use a different infix operator than `|` so that it’s not type-piracy:

```julia
julia> ∨(::Type{T}, ::Type{S}) where {T, S} = Union{T,S}
∨ (generic function with 1 method)

julia> simpleadd(a::(Float64∨Int64), b::(Float64∨Int64)) = a + b
simpleadd (generic function with 1 method)

julia> simpleadd(3, 4.2)
7.2

julia> methods(simpleadd)
# 1 method for generic function "simpleadd":
[1] simpleadd(a::Union{Float64, Int64}, b::Union{Float64, Int64}) in Main at REPL[4]:1

```

It’s probably still a bad idea, but I have to admit I’ve done crap similar to this when I got tired of writing `Union{whatever,Missing}`.

---

<div class="post-metadata">

**Author:** ![Usor](https://avatars.discourse-cdn.com/v4/letter/u/8dc957/32.png) [@Usor](https://discourse.julialang.org/u/Usor)\
**Post date:** [November 15, 2019, 9:14pm UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/8 "2019-11-15T21:14:10Z")

</div>

Hah hah hah! Beautiful! You people certainly surprise me. I still have to try it out, but it sure looks good.

But for some reason it gives me an error:  
`UndefVarError: Float64¥Int64 not defined`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 15, 2019, 10:40pm UTC](https://discourse.julialang.org/t/set-disjunction-for-arguments-if-defining-a-function/31124/9 "2019-11-15T22:40:57Z")

</div>

It looks like ¥ is parsed as part of an identifier (it’s as if you typed Float64XInt64, which is just a long name for a variable which does not exist). You can find a list of unicode symbols which are parsed as operators here: [https://github.com/JuliaLang/julia/blob/b29d95119d1f1bd485ba015c2bbfc9b08fcc09b9/src/julia-parser.scm#L16-L38](https://github.com/JuliaLang/julia/blob/b29d95119d1f1bd485ba015c2bbfc9b08fcc09b9/src/julia-parser.scm#L16-L38)
