func test (dynamicCase: {#a; #b}, pattern: {#a; #b}) : Bool{
switch(pattern) {
case(dynamicCase) {
return true
};
case (_) {
return false
}
}
};
How would I achieve something like this @claudio @nomeata ?
I want the first case to be either #a
or #b
depending on what i feed into test
I’m not sure I understand your question, but you can do something like this:
type MyVariant = {
#a;
#b;
#c
};
func isA(x: MyVariant) : Bool {
switch(x) {
case (#a) true;
case _ false;
}
};
func isB(x: MyVariant) : Bool {
switch(x) {
case (#b) true;
case _ false;
}
};
func isC(x: MyVariant) : Bool {
switch(x) {
case (#c) true;
case _ false;
}
};
func test<A>(x : A, f : A -> Bool) : Bool {
f(x);
};
assert(true == test(#a, isA));
assert(false == test(#a, isB));
assert(true == test(#a, func (x : MyVariant) : Bool {
isA(x) or isB(x);
}));
assert(false == test(#c, func (x : MyVariant) : Bool {
isA(x) or isB(x);
}));
1 Like
The test
wrapper is a bit redundant of course but it works if you need a common interface.
1 Like
For these simple tests, you can also just use equality.
The dynamic case you were asking for isn’t directly expressible with switch - that’s the price you pay for the benefit of compile-time coverage checking of cases - the case patterns need to be static, not dynamic.
2 Likes
That’s what I ended up doing, but it felt a bit odd that I needed to repeat this for every variant
If I have a lot of different variants in a variant this becomes a lot of code and a lot of work 
This should work! I’ll test and get back to you
1 Like