Modeling data within a union type

I have a Data Type

type State
    = Tp W X Y Z
   | ....

I’ve just realised that I will need an extra piece of data to this A. That’s going to involve changing all the helpers e.g. to pattern match on 5 _.

I’m wondering whether it would be better to switch to

type State
    = Tp {w: W, x: X, y: Y, z: Z}
   | ....

feels as though it would be easier to adjust and expand


The proper solution is probably situational. You can put all of them into a record. For example, in this game, I defined my main game type as:

type Game
    = IntroPhase IntroState
    | TowerPlacementPhase PlacementState
    | AttackPhase GameState
    | Victory GameState
    | Defeat GameState

IntroState, PlacementState, and GameState are all aliases for different records.

Depending on how the values group, you may want to make two records or even one record and one independent value. Consider this type:

type PersistableUser
  = Persisted Id User
  | Persisted User

User is an alias for a record.

1 Like

Thanks. I was really looking for any gotchas in going this other route, but I decided to try it anyway and so far it seems to work fine and simplify some parts of my code

1 Like

I think that switching to a record when you have more than 3 arguments would be an example of doing the right thing instead of the expedient thing.

Having more than 3 arguments leads to poorer quality code. Most of the time you are either cramming too much functionality into a function/tag or there is some kind of implicit structure that would benefit from being explicit.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.