Last active
December 6, 2017 01:25
-
-
Save brentvatne/76cfad349ca99f97cc01b001f4589cef to your computer and use it in GitHub Desktop.
Variant vs Polymorphic Variant
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
/* Read about variants in https://reasonml.github.io/guide/language/variant */ | |
/* Variant */ | |
type thing = | Int(string) | Float; | |
type otherThing = | Int(int) | Float; | |
let a = Int(1); | |
/* Type Error: This expression has type string but an expression was expected of type int */ | |
let b = Int("some string"); | |
switch(a) { | |
| Int(a) => Js.log(a) | |
| Float => Js.log("it's a float!") | |
}; | |
/* Polymorphic Variant */ | |
let a = `Int(1); | |
let b = `Int("some string"); | |
switch(a) { | |
| `Int(a) => Js.log(a) | |
| `Float => Js.log("it's a float!") | |
}; | |
switch(b) { | |
| `Int(b) => Js.log(b) | |
| `Float => Js.log("it's a float!") | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment