Open disnet opened 13 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.
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?
hmm, why not make the contract for all positions? So if you want the example to be valid:
arr:: [Num, Str, ...Any]
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.
Like arrays but not extensible? Maybe the syntax could be parens:
How to disambiguate though:
Is that a tuple or are we just over parenthesizing?