Skip to content

Instantly share code, notes, and snippets.

@clyfe
Created August 30, 2011 09:17
Show Gist options
  • Save clyfe/1180516 to your computer and use it in GitHub Desktop.
Save clyfe/1180516 to your computer and use it in GitHub Desktop.
CoffeeScript contracts
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