(no title)
disnet | 14 years ago
But we would still like to express and enforce some invariants about our code. And we'd like to do it in a more elegant way than constantly writing "if(argument === ...) then ... else ..."
Contracts also allow us to check invariants that might not result in a run-time exception but could lead to the code just being subtly wrong.
irrelative|14 years ago
Indeed it would be. That said, CoffeeScript is a compiler of sorts.
> And we'd like to do it in a more elegant way than constantly writing "if(argument === ...) then ... else ..."
This doesn't really seem to solve that problem. It just throws consistent errors if the types don't match. Your code would likely throw its own error if you didn't check the type manually. In my experience, native js errors are as easy or easier to track down that throwing errors yourself.
> Contracts also allow us to check invariants that might not result in a run-time exception but could lead to the code just being subtly wrong.
Ah, ok. That makes more sense. They're like python decorators, only single purpose.
disnet|14 years ago
The fact that we have a compiler doesn't give us all that much. Adding types to a dynamic language (where things can be monkey patched, evaled, and just generally mutated willy-nilly) is a hard problem (not impossible but definitely research [1 and 2 to start, but much more]).
>> And we'd like to do it in a more elegant way than constantly writing "if(argument === ...) then ... else ..."
> This doesn't really seem to solve that problem.
It solves the problem of elegance :)
> It just throws consistent errors if the types don't match. Your code would likely throw its own error if you didn't check the type manually. In my experience, native js errors are as easy or easier to track down that throwing errors yourself.
Key word there is likely. Like I mentioned, there are some errors that don't result in a run-time error and we might like to check for those too. As a silly example, consider a binary search tree [3] that you want to keep balanced. If you get the balance wrong, there's no immediate fault (no TypeError/NPE).
> Ah, ok. That makes more sense. They're like python decorators, only single purpose.
I guess you could say that. If we had an expressive enough decorator-like system for CoffeeScript we could probably implement contracts with it. CS doesn't have one of course so path of least resistance is to add the contract language extension directly.
[1] http://ecee.colorado.edu/~siek/blame-forall-2011.pdf
[2] http://dl.acm.org/citation.cfm?id=1863561
[3] https://github.com/disnet/contracts.coffee/blob/master/test/...