Is there a way to quickly check if a value is of a variant type? In the following example it would be great if I could write something shorter.such as keep(p => p == Additional(_))
Not sure you can’t use a wildcard in equality. You can create a helper though:
let isAdditional = p =>
switch p.value {
| Additional(_) => true
| _ => false
}
But I think it’s a good idea to make that switch exhaustive. Then, if you add another variant to the type, or remove a variant, you’re going to be forced to revisit the helper, and this could save you from your checks becoming nonsensical.
I agree, I think in most cases _ is a footgun. Ok it might be a bit annoying to list all the cases now but what’s it going to take you – 20 seconds? A minute? Compare that to the loss of knowledge at the type level of your program, and the loss of refactoring support from the compiler.
Could maybe do some higher order modelling though with GADT’s. Still not familiar enough with polymorphic variants though so just something to explore later!