Skip to content

Instantly share code, notes, and snippets.

@semperos
Created December 11, 2012 22:19
Show Gist options
  • Select an option

  • Save semperos/4262843 to your computer and use it in GitHub Desktop.

Select an option

Save semperos/4262843 to your computer and use it in GitHub Desktop.
Optional type checking (runtime contracts) in CoffeeScript via Ristretto (modified)
# Define your class
class Foo
add: (a, b) ->
a + b
@upper: (s) ->
s.toUpperCase()
class Bar
multiply: (a, b) ->
a * b
@lower: (s) ->
s.toLowerCase()
# Single type check
TypedUtil.applyType Foo, 'add', 'Int -> Int -> Int'
# Checking lots of types
TypedUtil.applyTypes Bar, ['multiply', 'Int -> Int -> Int'], ['lower', 'String -> String']
# You mess stuff up, you get informative errors:
Bar.lower(42);
# TypeError: upper
# Value: foo
# Expected: String
# Actual: Number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment