News for hackers. Stuff that matters.
Re:Contracts For CoffeeScript by clyfe on Tuesday August 30, 2011 @05:18AM (#2940674)
Can't this be implemented as a DSL? https://gist.github.com/1180516

    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
I think this can be extended to custom types too.
Re:Contracts For CoffeeScript by samth on Tuesday August 30, 2011 @07:43AM (#2940996)
First, I don't think what you show is a DSL -- it's just some functions. Rather, what Tim has implemented is a DSL.

Second, you need proxies to handle things like functions and mutable arrays.

Third, that there's a DSL for describing contracts is, I think, a large part of the point.