contracts.coffee icon indicating copy to clipboard operation
contracts.coffee copied to clipboard

Add support for tuples

Open disnet opened this issue 14 years ago • 4 comments

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?

disnet avatar Aug 30 '11 18:08 disnet

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.

gregwebs avatar Apr 18 '12 22:04 gregwebs

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?

disnet avatar Apr 18 '12 23:04 disnet

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

arr:: [Num, Str, ...Any]

gregwebs avatar Apr 19 '12 02:04 gregwebs

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.

disnet avatar Apr 19 '12 09:04 disnet