I’m trying to get my head around discriminated unions and record types; specifically how to combine them for maximum readability. Here’s an example – say a sports team can either have points (both league points and goal difference), or it can be suspended from the league in which case it has no points or goal difference. Here’s how I tried to express that:
type Points = { LeaguePoints : int; GoalDifference : int }
type TeamState =
| CurrentPoints of Points
| Suspended
type Team = { Name : string; State : TeamState }
let points = { LeaguePoints = 20; GoalDifference = 3 }
let portsmouth = { Name = "Portsmouth"; State = points }
The problem comes at the end of the last line, where I say ‘State = points’. I get ‘Expression was expected to have type TeamState but here has type Points’. How do I get around that?
1 Answer