0

Like for simple team and game types

type team =
  | Team(string);

type game1 =
  | Game(team, team);

type game2 = {
  team1: team,
  team2: team,
};
  1. What is the difference between game1 and game2?
  2. In real world when to declare type game like game1 or when to declare like game2?
  3. Are there any other ways this thing can be defined in Reason?
amitdigga
  • 6,550
  • 4
  • 26
  • 31
  • This is the kind of question that documentation, books and tutorials answer. It is too broad for Stack Overflow. See [ask] and the [help/on-topic]. [Real World OCaml](https://realworldocaml.org/) is a good book, available online. – glennsl Dec 09 '19 at 19:48
  • `game1` is called a "variant", or in type theory a "sum type". `game2` is a "record", or "product type". Look up [Algebraic Data Types](https://en.wikipedia.org/wiki/Algebraic_data_type) for a more general theoretic explanation of the differences between sum types and product types. – glennsl Dec 09 '19 at 19:50
  • Variant can be useful for pattern matching. Since there is only one possible `Game(team,team)` so whats the difference between `Game` and `game2`. Any link will also be helpful. – amitdigga Dec 10 '19 at 10:08
  • `Game` is a type constructor. In order to make a `game1`, you have to use one of its constructors- in this case you only have one. I particularly like these for modeling view state `type viewState = | Loaded(user) | Loading | Failed(string);` – Dennis Dec 20 '19 at 00:02
  • agree with @glennsl, but since Game from game1 has a payload (team, team) which somewhat like game2 without names for the two teams) it holds a product type, and since you have only one variant, i think its sematically the same. In this case i would go for game2, because you don't need to handle a single case all the time – farukg Mar 18 '20 at 18:49

0 Answers0