I’m creating a little card game using F# but I’m having a few problems due to circular type dependencies. I have the following card types (simplified):
type Monster =
{ Health: int
Attack: int
SkillText: string option }
type Spell =
{ EffectText: string }
type Kind =
| Monster of Monster
| Spell of Spell
type Card =
{ Name: string
Image: string
Kind: Kind
IsPlayer: bool
CanPlay: Board -> bool }
The idea is that, when creating a new card, it must define the conditions needed for it to be played based on the current board state.
The board type is
and Board =
{ PlayerHand: Hand
EnemyHand: Hand
PlayerDeck: Deck
EnemyDeck: Deck
PlayerField: Field
EnemyField: Field
PlayerGraveyard: Graveyard
EnemyGraveyard: Graveyard }
My problem is, the types inside the Board all depend on the Card
type, so I need to define it after the Card type, but the Card type depends on the Board type. I know that I could use generics to undo the cyclic dependency, but I have the following problem with it:
If I define a Board<'Card>
type I would need to make all the types referenced by Board as generic. That not only seems verbose to me, but it also doesn’t make a lot of sense in my application. I’ll never make a hand of anything that isn’t a Card. I know that I could do Card<'Board>
instead, but that seems less intuitive and the same problems I have with Board<'Card>
apply.
Is there any way to solve this problem without generics while staying purely functional?