[swift-evolution] Sparse (fixed-size) array literal syntax

Daryle Walker darylew at mac.com
Fri Jun 2 02:22:00 CDT 2017


> On Jun 2, 2017, at 2:11 AM, Jaden Geller <jaden.geller at gmail.com> wrote:
> 
> Comments inline.
> 
>> On Jun 1, 2017, at 10:49 PM, Daryle Walker via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Current array literal syntax (i.e. “[a, b, c]”) works for dense and/or linear arrays, but isn’t so great later on when we add fixed-size arrays and the defined (non-zero) elements are sparse and/or the array is multi-dimensional (not nested). In these cases I’m thinking of leading each element with its coordinate:
>> 
>> … 6: a, …  // complete value, linear array
> 
> You can already do this with a dictionary since a Int can be used as the key type.
> 
>> … (1, 2): b, …  // complete value, multi-dimensional array
> 
> This would be possible if tuples of hashable types were considered hahable. Right now, you could define some point type.
> 
>> … (let x, let y) where y % 2 == 0: c * y + x, …  // pattern of qualifying coordinates
> 
> You can build this sort of thing using an initializer that takes a function. You wouldn’t get this sugar, but I don’t think it is necessary.
> 
>> … default: d, …  // when no other initializer covers an element (Use “_” instead?)
> 
> This one is a bit harder. I think it would be reasonable for there to exist some subtype of the dictionary literal type that also included information about a default value, but I think this should be motivated by the Swift standard library (e.g. a defaultable dictionary type).
> 
> Right now, you can just make literal syntax default to a `nil` default value, and then you can define a function that nil-coalesces the default value. This is definitely less elegant, but I don’t think it matters a whole lot.
> ```
> Sparse([1: “foo”, 5: “bar”, 100: “baz”], default: “”)
> ```

I’m not asking how to do this in current Swift with current types, but fixed-sized arrays in a future Swift. I’m asking if this mixing of array- and dictionary-literals for these advanced-array literals would be too hard for compiler implementors to implement.

— 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 

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


More information about the swift-evolution mailing list