The bit of trickiness here is that the handler will receive a payload which may be any of the payload types of the input actions. To specify this under the type system, we need an overload for each number of arguments:
This means that the function will only be typechecked if it has a number of arguments up to the number of overloads we define. Still, this is probably worth it if we assume most people won't want to put the same handler on more than eight or so different action types.
One remaining question is how to deal with a number of action types greater than the limit. We have three options:
Don't allow this at all. It is a type error to pass more than <typechecked-limit> action types to .cases. The most limiting, but possibly the least confusing of these options.
Allow it, but require that the handler treat the payload as void. Callers can still put a common handler on the actions as long as they don't use any data from the payload.
Allow it, but the handler's payload is of type any. The most permissive, but allows incorrectly typed code.
As an alternative to variadic arguments, we could instead use chaining once again, which would look something like:
This is nicer as far as the type system is concerned (it works for any number of arguments, and avoids many variadic overloads), but is a fair bit harder to understand.
I'm going to think over this a bit then implement one of the solutions. Feel free to chime in if you have opinions about this.
Currently, if several action types should have the same handler, the only way to do it is with repetition:
It would be preferable to have something like
The bit of trickiness here is that the handler will receive a payload which may be any of the payload types of the input actions. To specify this under the type system, we need an overload for each number of arguments:
This means that the function will only be typechecked if it has a number of arguments up to the number of overloads we define. Still, this is probably worth it if we assume most people won't want to put the same handler on more than eight or so different action types.
One remaining question is how to deal with a number of action types greater than the limit. We have three options:
<typechecked-limit>
action types to.cases
. The most limiting, but possibly the least confusing of these options.void
. Callers can still put a common handler on the actions as long as they don't use any data from the payload.any
. The most permissive, but allows incorrectly typed code.As an alternative to variadic arguments, we could instead use chaining once again, which would look something like:
This is nicer as far as the type system is concerned (it works for any number of arguments, and avoids many variadic overloads), but is a fair bit harder to understand.
I'm going to think over this a bit then implement one of the solutions. Feel free to chime in if you have opinions about this.