Open geohuz opened 5 years ago
Yeah, this is pretty cool: I like it a lot in Elixir.
However, this is an independent feature: we can have both the match branch structure and matchable overloads.
Matchable overloads would look a bit like this: https://github.com/alehander42/poly
On the other hand in typed languages it leads to some boilerplate: as you usually have to type the whole header with types etc, so using a single match in a function still would be idiomatic
new logic can be dynamically added.
this is also not very desirable: having all the branches for the same function in one place is very important.
matchproc f(n: int, m: int):
f(2, 0):
0
f(2, m):
m
f(n, m):
n
maybe this can be possible, but still not sure if worth it?
Example from Julia's patterndispatch:
actually the @pattern expands to the following code:
You can see it is a really powerful idea which decoupling the dispatch logic from giant if/else block and new logic can be dynamically added. I would say it's a general pattern of multi-method which called predicate dispatch: https://en.wikipedia.org/wiki/Predicate_dispatch