disnet / contracts.coffee

Contracts for CoffeeScript
http://disnet.github.io/contracts.coffee/
MIT License
216 stars 6 forks source link

Add support for tuples #4

Open disnet opened 13 years ago

disnet commented 13 years ago

Like arrays but not extensible? Maybe the syntax could be parens:

tup :: (Num, Bool)
tup = [42, true]

How to disambiguate though:

f :: ((Num, Bool)) -> Num
f = ...

Is that a tuple or are we just over parenthesizing?

gregwebs commented 12 years ago

a javascript array is a tuple, right? And for an array with a singular type we have [...Type]. Seems that this is satisfied already with a syntax matching javascript.

disnet commented 12 years ago

Currently contracts on arrays just restrict the array indexes they specify. So this is allowed:

arr :: [Num, Str]
arr = [42, "foo"]
arr[10] = 24

But it might be nice to have a contract that limits the array positions to just those specified in the contract. So for example a 2-tuple contract would allow js arrays that had two elements but fail for arrays with three elements.

Though, this might not be something we want explicit syntax for. Perhaps the es5 property descriptors (extensible, configurable, etc.) are sufficient?

gregwebs commented 12 years ago

hmm, why not make the contract for all positions? So if you want the example to be valid:

arr:: [Num, Str, ...Any]
disnet commented 12 years ago

Good point. When writing a contract it's probably most common for extensible arrays to be homogeneous (covered by [...C]) and non-extensible arrays to be heterogeneous (covered by [C_1, C_2]). So when you want extensible and heterogeneous do [C_1, C_2, ...Any].

So this involves changing [C_1, C_2] to enforce non-extensibility. Pretty straightforward.