-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Labels
Description
Consider a variant-definition using typecase like the following:
@variant
class Maybe:
RGB = Tuple(float, float, float)
RGBA = Tuple(float, float, float, float)If we want to write code that works for both RGB and RGBA, we would like to implement a function that works on both cases. A very traditiional and unsatifying solution would be to dispatch over those types as in
def is_monochrome(color):
if isinstance(color, Color.RGB):
# ....
else:
# ...whereas if we were using Rust, we would probably have something like
fn is_monochrome(color) -> bool {
match (color) {
RGB(f1,f2,f3) => { /* ... */},
RGBA(f1,f2,f3, f4) => { /* ... */},
}
}@Color.method
def is_monochrome():
pass
@is_monochrome.case(Color.RGB)
def is_monochrome(c):
f1, f2, f3 = c
return f1 == f2 == f3
@is_monochrome.case(Color.RGBA)
def is_monochrome(c):
f1, f2, f3, f4 = c
return f1 == f2 == f3 == f4Or something of the like. We could investigate whether multimethods libraries are suitable for this use case. That approach would have some resemblance to multiple dispatch / multimethods, so we should probably consider whether libraries like https://pypi.python.org/pypi/multimethod/ are applicable.