6

What is the idiomatic way to write the equivalent of an algebraic data type in Crystal? E.g. In Haskell I might have

data Stage = StageInitial String | StageFinished String

So I want to have two stages, each which has a string payload. Later on I want to pattern match on the stage.

How would you write this in Crystal?

Sebastian
  • 2,249
  • 17
  • 20

1 Answers1

11

You can roughly emulate it with

record StageInitial, data : String
record StageFinished, data : String
alias Stage = StageInitial | StageFinished

then pattern match with case.

Stephie
  • 3,135
  • 17
  • 22