Sebastian
Sebastian

Reputation: 2279

Crystal equivalent to algebraic data types

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?

Upvotes: 6

Views: 454

Answers (1)

Stephie
Stephie

Reputation: 3175

You can roughly emulate it with

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

then pattern match with case.

Upvotes: 11

Related Questions