Skip to content

Instantly share code, notes, and snippets.

@brentvatne
Last active December 6, 2017 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentvatne/76cfad349ca99f97cc01b001f4589cef to your computer and use it in GitHub Desktop.
Save brentvatne/76cfad349ca99f97cc01b001f4589cef to your computer and use it in GitHub Desktop.
Variant vs Polymorphic Variant
/* 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