Crystal equivalent to algebraic data types
Asked Answered
A

1

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?

Anatolia answered 27/2, 2018 at 0:12 Comment(0)
A
11

You can roughly emulate it with

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

then pattern match with case.

Austroasiatic answered 27/2, 2018 at 0:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.