[swift-evolution] Sparse (fixed-size) array literal syntax
John McCall
rjmccall at apple.com
Fri Jun 2 11:45:15 CDT 2017
> On Jun 1, 2017, at 10:49 PM, Daryle Walker via swift-evolution <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
> … (1, 2): b, … // complete value, multi-dimensional array
> … (let x, let y) where y % 2 == 0: c * y + x, … // pattern of qualifying coordinates
> … default: d, … // when no other initializer covers an element (Use “_” instead?)
>
> A complete coordinate beats a pattern, which beats a default. The issue I see here is that I’m using a colon as a separator between the coordinate expression and the value expression. Would that interfere with dictionary literal syntax? Would it help the we’ll most likely have to demand that the object receiving the literal has to have its type specified (with whatever syntax we agree on), as either a declared object with a type annotation or a function parameter (for a function either without overload or with the variant used otherwise made clear)?
"Literals" like this are obviously complicated to the point that you would be much better off just using an initializer that takes a function to construct each element. There's no point in reinventing switches in the array-literal syntax.
John.
More information about the swift-evolution
mailing list