I am just at the beginning of my F# journey so please bear with me in the below.
I was wondering whether it is possible and meaningful to make exhaustive pattern matching on subsets of a DU.
type Fruit = Apple | Banana | Coconut | Grape | Hazelnut | Orange | Strawberry
I want to put this into subsets with a multi case total active pattern. E.g.:
let (|Peel|Crack|Wash|) = function
| Banana | Grape | Orange → Peel
| Coconut | Hazelnut → Crack
| Apple | Strawberry → Wash
Say I want to know how I should open a fruit that needs to be cracked, e.g. with a hammer or a nut cracker. How can I make an exhaustive pattern check for all fruits that should be cracked, so that adding another crackable fruit I get a compile error until I state whether a hammer, a nut cracker or a third tool should be used? On the other hand I don’t want to have to do anything if I add a fruit that should not be cracked.
Something along the lines of this but without the Na’s and the _ and without having to worry about Peel and Wash fruits:
let (|Hammer|NutCracker|Na|) fruit =
match fruit with
| Peel → Na
| Crack →
match fruit with
| Coconut → Hammer
| Hazelnut → Nutcracker
| _ → Na
| Wash → Na
One possibility is to create multiple DUs but I want to be able to create more partitions/views on the fruit DU. E.g.:
let (|Round|Oblong|Other|) = function
| Apple | Grape | Orange → Round
| Banana | Coconut → Oblong
| Hazelnut | Strawberry → Other
let (|Soft|Medium|Hard|) = function
| Strawberry | Banana → Soft
| Grape | Orange → Medium
| Apple | Coconut | Hazelnut → Hard
Is something like this achievable or even advisable in F#?
I hope the above makes my intention clear. If not, please let me know and I will try to elaborate.