Created
December 11, 2012 22:19
-
-
Save semperos/4262843 to your computer and use it in GitHub Desktop.
Optional type checking (runtime contracts) in CoffeeScript via Ristretto (modified)
This file contains hidden or 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
| # 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