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 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
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
Is there any reason for not having this? In the sense of defining types with
a set difference being discouraged?
See also this thread, where I wanted to exclude Bool
inputs from Integer
:
const ExceptBool = Union{Signed, Unsigned}
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)
foo(::Integer) = bar()
foo(::Bool) = error()
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.
Sure, often there are many ways, each with its own distinct set of advantages and limitations.