[swift-evolution] Proposal: Contiguous Variables (A.K.A. Fixed Sized Array Type)

Dany St-Amant dsa.mls at icloud.com
Thu Jan 28 20:40:21 CST 2016


> Le 28 janv. 2016 à 19:56, Joe Groff via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> 
>> On Jan 28, 2016, at 4:04 PM, Haravikk <e-mail at haravikk.me <mailto:e-mail at haravikk.me>> wrote:
>> 
>> 
>>> On 28 Jan 2016, at 22:37, Joe Groff via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> 
>>> 
>>>> On Jan 28, 2016, at 2:36 PM, Jacob Bandes-Storch <jtbandes at gmail.com <mailto:jtbandes at gmail.com>> wrote:
>>>> 
>>>> I like this idea, but the syntax seems dangerously close to a call site for  "func *(lhs: Int, rhs: Any.Type)"  (which is obviously ill-advised, but it is allowed).
>>>> 
>>>> Maybe we could take advantage of something which would be very invalid under the current grammar, namely (n T) rather than (n * T):
>>>> 
>>>>    let values: (4 Int) = (1, 2, 3, 4)
>>> 
>>> Sure, or we could lift (4 x Int) from LLVM IR's syntax.
>> 
>> How about:
>> 
>> 	let values:Int[4] = (1,2,3,4)
>> 
>> While it looks a bit like a subscript, it doesn’t make sense in a type declaration at present, so could be a good way to define restrictions of this type (we could even extend it to collections later). If the similarity is too close then:
>> 
>> 	let values:(Int[4]) = (1,2,3,4)
>> 
>> Could work too? Just some alternatives anyway, as I like the idea.
> 
> This kind of syntax doesn't compose well with other type productions. If you parse Int[N][M] naively as (Int[N])[M], then you end up with an array of M (array of N (Int)), which ends up subscripting in the opposite order, array[0..<M][0..<N]. C works around this by flipping the order of multiple array indices in a type declaration, so int [n][m] is really (int [m]) [n], but this doesn't work well for Swift, which has other postfix type productions—how would Int[N]?[M] parse? Choosing a prefix notation for fixed-sized array bounds is better IMO to avoid these pitfalls.

Hmm… Two dimensions tuples...
let matrix: (2 (4 Int)) = ( (1,2,3,4), (5,6,7,8) )
print(matrix.0.0, matrix.1.3) // display 1 and 8
// Output based on let matrix = ((1,2,3,4), (5,6,7,8))

The type is easier to read with LLVM IR syntax mentioned by Joe
let matrix: (2 x (4 x Int)) = ( (1,2,3,4), (5,6,7,8) )

A one dimensional tuple is supposed to be contiguous, but does the same behaviour apply to tuples of tuples?

Dany


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160128/5f75b1d4/attachment.html>


More information about the swift-evolution mailing list