5 ms·
Why not use something in the form of SingleDispatch on the typed key? This is a very simple pattern with Python, instead of passing literals, create the differ
by PartiallyTyped 5y ago
Why not use something in the form of SingleDispatch on the typed key?
This is a very simple pattern with Python, instead of passing literals, create the different literals as types, and make the function accept a union of said types. Then use `functools.singledispatch`, to map each type to the correct function. This results in three separate functions, just as one would do with algebraic data types.
Or, instead of singledispatch, use dictionary mapping `type(key)->Callable`, and pass the arguments there, making the graph identical to the second.
- incrudible 5y agoOr, maybe the original code was actually totally fine and in fact the simplest and most obvious solution.
- PartiallyTyped 5y agoLiterals should be avoided since they can get mistyped unless the compiler can enforce that.
- incrudible 5y agoThe TypeScript compiler can enforce that, notice how the type annotation in the example is `'admin' | 'customer'`, not `string`. It's also possible to give this type a name: type UserCreationType = 'admin' | 'customer'
- PartiallyTyped 5y agoI see. Then I am joining the club where the first one was perfectly fine.
- beebeepka 5y agoYup, typescript is pretty good when it comes to this kind of stuff. This was one of the very few things I tried when I first started with TS.