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

davesweeris at mac.com davesweeris at mac.com
Tue Feb 9 13:33:15 CST 2016

> On Jan 28, 2016, at 20:00, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>> On Jan 28, 2016, at 7:57 PM, Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>>> On Jan 28, 2016, at 5:07 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> I find 
>>> let values: (4 x Int) = (1, 2, 3, 4)
>>> to be adequately cromulent. I believe this approach to be:
>>> * Readable, even to someone unfamiliar with the syntax
>>> * The parens before the assignment suggest something to do with tuples, and the numbers match the arity after the assignment
>>> * The type is preserved in-place
>>> * It's compact, elegant, simple
>> +1.  I like this syntax too, and with Joe’s other proposed extensions, it would all fit together nicely.  Anyone interested in writing up a proposal?
> One other tweak worth considering: instead of subscript being defined on a tuple as returning a common type, a more modest proposal would be that subscript is defined on tuples that:
> a) have no element labels.
> b) all have the same element type.
> This would solve the fixed size array use-case, be much easier to implement, and not have surprising performance issues promoting things to Any.  It is also consistent with the fact that we don’t infer the type of [Int(), Float()] to [Any].
> -Chris

(Sorry to go so far back… I started replying to this on probably the 29th and somehow forgot about it.)

Out of curiosity, why would the subscript of non-homogeneous tuple have to return an "Any”? If we declare this:
let grohl = (0, “foo”, “fighters”, 0.0)

Why couldn’t subscript (and $0 in map, for that matter) return a type that’s the “intersection” of Int, String, and Double?
// compiler expands this to (grohl.0.hashValue, grohl.1.hashValue, grohl.2.hashValue, grohl.3.hashValue)
let thatsJustCrazyTalk = grohl.map {$0.hashValue} // no error because Int, String, and Double all have a hashValue property…

It seems to me that figuring out what’s legal for $0 wouldn’t be that hard… I mean, at the risk of publicizing my ignorance regarding the compiler’s internal workings, in my head, at least, the compiler maintains a set of valid functions/properties for each type, and $0’s set would literally just be the intersection of the sets associated with the types in the tuple.

Is it way more complicated than that? (One of these days I’m going to figure out how LLVM works so that I won’t have ask questions like that.)

- Dave Sweeris

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

More information about the swift-evolution mailing list