Last active
October 8, 2019 03:54
-
-
Save kennetpostigo/e1e07d5232395ad5420f0ecb12fe65a1 to your computer and use it in GitHub Desktop.
Reason BST Primer for Blog Post algebraic data types + pattern matching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type worker = { | |
name: string, | |
age: int, | |
}; | |
type workStatus = | |
| Unemployed(worker) | |
| Fulltime(worker) | |
| Parttime(worker); | |
type fetchResults = | |
| Error(string) | |
| Payload(workStatus); | |
let getWorker: string => fetchResults = name => Payload(Fulltime({name, age: 17})); | |
/* fake request */ | |
let findDoug = getWorker("Doug"); | |
let res = | |
switch (findDoug) { | |
| Payload(Fulltime({name, age})) => name ++ " is a fulltime worker" | |
| Payload(Parttime({name, age})) => name ++ " is a partime worker" | |
| Payload(Unemployed({name, age})) => name ++ " is sadly unemployed" | |
| Error(message) => failwith(message) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment