3 ms·
Can't this be implemented as a DSL? https://gist.github.com/1180516 https://gist.github.com/1180516 guardArgs = (def, args) -> i = 0 for arg, t
by clyfe 15y ago
Can't this be implemented as a DSL? https://gist.github.com/1180516 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.
- samth 15y agoFirst, 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.