Hi all,
This is my first time posting so please let me know if I’m framing this question correctly! This may have been answered somewhere else, but I couldn’t find any documentation on this or tests in Github around this. I was wondering if there was an easy way to use “generics” in switch
statements. For example, I’m trying to get better at rescript by creating a Texas Holdem game and this is what I have for comparing scores:
let compare = (score1, score2) => switch(score1, score2) {
| (#StraitFlush(x), #StraitFlush(y)) => compareFaces(x, y)
| (#StraitFlush(_), _) => -1
| (#FourOfAKind(_, _), #StraitFlush(_)) => 1
| (#FourOfAKind('a, x), #FourOfAKind('a, y)) => compareFaces(x, y)
| (#FourOfAKind(x, _), #FourOfAKind(y, _)) => compareFaces(x, y)
| (#FourOfAKind(_, _), _) => -1
| (#FullHouse(_, _), #StraitFlush(_) | #FourOfAKind(_, _)) => 1
| (#FullHouse('a, x), #FullHouse('a, y)) => compareFaces(x, y)
| (#FullHouse(x, _), #FullHouse(y, _)) => compareFaces(x, y)
| (#FullHouse(_, _), _) => -1
| (#Flush(_, _, _, _, _), #StraitFlush(_) | #FourOfAKind(_, _) | #FullHouse(_, _)) => 1
| (#Flush('a, 'b, 'c, 'd, x), #Flush('a, 'b, 'c, 'd, x)) => compareFaces(x, y)
| (#Flush('a, 'b, 'c, x, _), #Flush('a, 'b, 'c, y, _)) => compareFaces(x, y)
| (#Flush('a, 'b, x, _, _), #Flush('a, 'b, y, _, _)) => compareFaces(x, y)
| (#Flush('a, x, _, _, _), #Flush('a, y, _, _, _)) => compareFaces(x, y)
| (#Flush(x, _, _, _, _), #Flush(y, _, _, _, _)) => compareFaces(x, y)
| (#Flush(_, _, _, _, _), _) => -1
| (#Strait(_), #StraitFlush(_) | #FourOfAKind(_, _) | #FullHouse(_, _) | #Flush(_, _, _, _, _)) => 1
| (#Strait(x), #Strait(y)) => compareFaces(x, y)
| (#Strait(x), _) => -1
| (#ThreeOfAKind('a, 'b, x), #ThreeOfAKind('a, 'b, y)) => compareFaces(x, y)
| (#ThreeOfAKind('a, x, _), #ThreeOfAKind('a, y, _)) => compareFaces(x, y)
| (#ThreeOfAKind(x, _, _), #ThreeOfAKind(y, _, _)) => compareFaces(x, y)
| (#ThreeOfAKind(_, _, _), #TwoPair(_, _, _) | #Pair(_, _, _, _), #HighCard(_, _, _, _, _)) => -1
| (#ThreeOfAKind(_, _, _), _) => 1
| (#TwoPair('a, 'b, x), #TwoPair('a, 'b, y)) => compareFaces(x, y)
| (#TwoPair('a, x, _), #TwoPair('a, y, _)) => compareFaces(x, y)
| (#TwoPair(x, _, _), #TwoPair(y, _, _)) => compareFaces(x, y)
| (#TwoPair(_, _, _), #Pair(_, _, _, _) | #HighCard(_, _, _, _, _)) => -1
| (#TwoPair(_, _, _), _) => 1
| (#Pair('a, 'b, 'c, x), #Pair('a, 'b, 'c, y)) => compareFaces(x, y)
| (#Pair('a, 'b, x, _), #Pair('a, 'b, y, _)) => compareFaces(x, y)
| (#Pair('a, x, _, _), #Pair('a, y, _, _)) => compareFaces(x, y)
| (#Pair(x, _, _, _), #Pair(y, _, _, _)) => compareFaces(x, y)
| (#Pair(_, _, _, _), #HighCard(_, _, _, _, _)) => -1
| (#Pair(_, _, _, _), _) => 1
| (#HighCard('a, 'b, 'c, 'd, x), #HighCard('a, 'b, 'c, 'd, y)) => compareFaces(x, y)
| (#HighCard('a, 'b, 'c, x, _), #HighCard('a, 'b, 'c, y, _)) => compareFaces(x, y)
| (#HighCard('a, 'b, x, _, _), #HighCard('a, 'b, y, _, _)) => compareFaces(x, y)
| (#HighCard('a, x, _, _, _), #HighCard('a, y, _, _, _)) => compareFaces(x, y)
| (#HighCard(x, _, _, _, _), #HighCard(y, _, _, _, _)) => compareFaces(x, y)
}
As you can see I’m trying to use the generics notation “`a” when I’m trying to tell rescript, “I don’t care what the value is as long as the values are the same”. But this causes a compiler error. Is the only way to do something like this using guard statements? If so, do you know if there is a less verbose of writing this block without a ton of redundant kickerA1 == kickerB1 && kickerA2 == kickerB2 && ...
Thank you so much!