Created
August 30, 2011 09:17
-
-
Save clyfe/1180516 to your computer and use it in GitHub Desktop.
CoffeeScript contracts
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
guardArgs = (def, args) -> | |
i = 0 | |
for arg, type of def | |
throw new Error("bad type of #{arg}") unless typeof args[i] == type | |
i++ | |
typeEnsure = (def, f) -> -> | |
guardArgs(def, arguments) | |
f.apply @, arguments | |
myFunc = typeEnsure {name: 'string', i: 'number'}, (name, i) -> | |
console.log name, i | |
myFunc("foo", 1) # ok | |
myFunc("foo", "x") # Error: bad type of i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment