Skip to content

Instantly share code, notes, and snippets.

@sinelaw
Created September 9, 2015 20:26
Show Gist options
  • Save sinelaw/f1c47f9f2562e16de817 to your computer and use it in GitHub Desktop.
Save sinelaw/f1c47f9f2562e16de817 to your computer and use it in GitHub Desktop.
type case simulated using polymorphic sums
foo :: +{ Num : Number, Str : String | r } -> Number
function foo(x) {
if (typeof x == Number) { .. x is a number here! ...; return 0 }
else if (typeof x == String) { .. a string here ..; return 1 }
else { .. }
}
\case x of
Num n -> ... ; 0
Str s -> ... ; 1
_ -> ...
addOne :: +{ Num : Number } -> +{ Num : Number }
function addOne(x) { return x + 1; }
34 :: forall r. +{ Num : Number | r }
'bla' :: forall t. +{ Str : String | t }
foo(34) ===> here instantiated to: foo :: +{ Num : Number, Str : String } -> Number , and 34 :: +{ Num : Number, Str : String }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment