| Docs Build | |
| Documentation | |
| GHA CI | |
| Code Coverage | |
| Bors enabled |
DispatchedTuples are like immutable dictionaries (so, they're technically more like NamedTuples) except that the keys are instances of types. Also, because DispatchedTuples are backed by tuples, they are GPU-friendly.
There are two kinds of DispatchedTuples with different behavior:
┌────────────────────────────────────┬───────────────────────────┬────────────────────┐
│ Return value │ DispatchedTuple │ DispatchedSet │
│ │ (non-unique keys allowed) │ (unique keys only) │
├────────────────────────────────────┼───────────────────────────┼────────────────────┤
│ Type │ Tuple │ Value │
│ Unregistered key (without default) │ () │ error │
│ Unregistered key (with default) │ (default,) │ default │
│ Duplicative key │ all registered values │ one value │
└────────────────────────────────────┴───────────────────────────┴────────────────────┘
DispatchedTuple and DispatchedSets have three constructors:
- A variable number of (vararg)
Pairs + keyword default - A
TupleofPairs + positional default - A
Tupleof 2-elementTuples (the first element being the "key", and the second the "value") + positional default
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].
A default value, if passed to DispatchedTuple and DispatchedSet, is returned for any unrecognized keys as shown in the table above.
Here is an example in action
julia> using DispatchedTuples
julia> struct Foo end;
julia> struct Bar end;
julia> struct Baz end;
julia> dtup = DispatchedTuple((
Pair(Foo(), 1),
Pair(Foo(), 2),
Pair(Bar(), 3),
))
DispatchedTuple with 3 entries:
Foo() => 1
Foo() => 2
Bar() => 3
default => ()
julia> dtup[Foo()]
(1, 2)
julia> dtup[Bar()]
(3,)
julia> dtup[Baz()]
()