I created a package at GitHub - mkitti/OrUnions.jl: Julia package to evaluate alternate Union syntax via | and ∨ . To avoid piracy I used ∨
(\vee) rather than |
.
julia> 0x5::(UInt8 ∨ Int8)
0x05
julia> f(x::(Int8 ∨ UInt8 ∨ Int)) = x
f (generic function with 1 method)
If you really want to use |
, I created a macro called @orunion
.
julia> @orunion function foo(::Int8 | UInt8 | Int16) end
foo (generic function with 1 method)
julia> methods(foo)
# 1 method for generic function "foo" from Main:
[1] foo(::Union{Int16, Int8, UInt8})
@ REPL[34]:1
julia> @macroexpand @orunion function foo(::Int8 | UInt8 | Int16) end
:(function foo(::Union{Int16, UInt8, Int8})
#= REPL[36]:1 =#
#= REPL[36]:1 =#
end)
julia> @orunion bar(x::Int8 | UInt8, y::Int16 | UInt16, z::(Int8 | UInt8) | Int) = nothing
bar (generic function with 1 method)
julia> methods(bar)
# 1 method for generic function "bar" from Main:
[1] bar(x::Union{Int8, UInt8}, y::Union{Int16, UInt16}, z::Union{Int64, Int8, UInt8})
@ REPL[38]:1
julia> @macroexpand @orunion bar(x::Int8 | UInt8, y::Int16 | UInt16, z::(Int8 | UInt8) | Int) = nothing
:(bar(x::Union{UInt8, Int8}, y::Union{UInt16, Int16}, z::Union{Int, Union{UInt8, Int8}}) = begin
#= REPL[40]:1 =#
nothing
end)
julia> methods(@orunion (x::Int8 | UInt8)->5)
# 1 method for anonymous function "#35":
[1] (::var"#35#36")(x::Union{Int8, UInt8})