Reading this guide: Types as Sets · An Introduction to Elm
I have the same confusion mentioned by the OP here: Types and Sets - Cardinality of Custom type - #2 by Punie
Like the OP, I was thinking about the Bool
value being part of a returned tuple (so I was imagining a cardinality of 6 if not 7):
| Res | Bool | Color | Equiv |
|----:|------:|------:|-------------------|
| Ok | T | R | Ok (True, Red) |
| Ok | T | G | Ok (True, Green) |
| Ok | T | B | Ok (True, Blue) |
| Ok | F | R | Ok (False, Red) |
| Ok | F | G | Ok (False, Green) |
| Ok | F | B | Ok (False, Blue) |
| Err | _ | _ | Err String |
|-----|-------|-------|-------------------|
| | Total | 7 |
|-----|-------|-------|
However, if Bool
represents the Ok
/Err
state, I still fail to see how I should consider a cardinality of 5. In my mind, I see those branches.
| Res | Bool | Color | Equiv |
|----:|------:|------:|------------|
| Ok | T | R | Ok Red |
| Ok | T | G | Ok Green |
| Ok | T | B | Ok Blue |
| Err | F | _ | Err String |
|-----|-------|-------|------------|
| | Total | 4 |
|-----|-------|-------|
I suppose this means that mean that:
Ok(Red | Green | Blue)
is to be considered as part of an element of the “set” of possible values?
I think I’m a little confused since I’m thinking in terms of truth tables, and that’s how I tend to think about multiplying the complexity of code branches and I relate this to if/else
statements.
Could anyone clarify this for me? I’d like to understand how considering cardinality could help me write better code.
I understand how ORing a type means you have to add the elements of the set to obtain the cardinality, but I fail to visualise what’s going on here.