API
DispatchedTuples.AbstractDispatchedTuple
— TypeAbstractDispatchedTuple{T <: Tuple, D}
An abstract dispatch tuple type, for sub-typing dispatched tuples.
DispatchedTuples.DispatchedTuple
— TypeDispatchedTuple(tup[, default_value])
A dispatch-able tuple.
DispatchedTuple
takes a Tuple
of Pair
s, where the first
field of the Pair
(the "key") is an instance of the type you want to dispatch on. The second
field of the Pair
is the quantity (the "value", which can be anything) returned by dtup[key]
.
If a DispatchedTuple
has non-unique keys, then all values are returned in the returned Tuple
.
The second (optional) argument to DispatchedTuple
is a default value, which is returned for any unrecognized keys. If the default value is not given, then dtup[key]
on an unregistered key will return an empty tuple.
For convenience, DispatchedTuple
can alternatively take a Tuple
of two-element Tuple
s.
Example
using DispatchedTuples
struct Foo end;
struct Bar end;
struct Baz end;
dtup = DispatchedTuple((
Pair(Foo(), 1),
Pair(Foo(), 2),
Pair(Bar(), 3),
));
dtup[Foo()] # returns (1, 2)
dtup[Bar()] # returns (3,)
dtup[Baz()] # returns ()
DispatchedTuples.DispatchedSet
— TypeDispatchedSet(tup[, default_value])
Similar to DispatchedTuple
, except:
- keys must be unique.
- returns the value, and not a tuple of values.
- throws an error in
dtup[key]
if the key is not unique (without a default value).