SE-0483: `InlineArray` Literal Syntax

Considering the following bare options:

n x T , n of T , n * T , n # T , T[n] , [n]T , [n; T] , n T, n inline T

I think that we need to evaluate the following.

• Does the keyword, operator or delimiter help convey "N units of type T" or "N units of value N" to the reader.

• Does the keyword, operator or delimiter have the flexibility to work at the declaration site as well as the use site.

ex: var a: [10 of Int] and a = [10 of 10]

• Can the keyword be expanded into other environments (tuples).

ex: var a: (5 of Int) is the same as var a: (Int, Int, Int, Int, Int)
ex: a = (5 of 2) is the same as a = (2,2,2,2,2)

• How does it look and work in the context of opaque types.

ex: func build() -> [some of some Type]

• How does it look with (I forget the name but _ implied elements )

• Can the keyword be used in other ways.

ex: var x: [5 of Int] = [6, 3 of 4, 2] for var x: [5 of Int] = [6,4, 4, 4, 2]

The purpose of including some of these not yet proposed features is to examine how useful a new keyword can be, and allowing us to think forward.


Putting it to the test

Let's try n # T, n x T and n T as an example.

n # T

var a: [10 # Int] and a = [10 # 10]
var a: (5 # Int) for var a: (Int, Int, Int, Int, Int)
a = (5 # 2) for a = (2,2,2,2,2)
var x: [5 # Int] = [6, 3 # 3, 2]
var x: [6 # Int] = [1 , 4 # 4 * 2, 3] // [1 , 8, 8, 8, 8, 3]
func build() -> [some # some Type]

For me, # just doesn't convey the message "N units of type T" or "N units of value N." Outside of that I don't have much against it. It technically works. I suppose it's strong association with Macros and #if statements disqualifies it for me.

n x T

var a: [10 x Int] and a = [10 x 10]
var a: (5 x Int) for var a: (Int, Int, Int, Int, Int)
a = (5 x 2) for a = (2,2,2,2,2)
var x: [5 x Int] = [6, 3 x 3, 2]
var x: [6 x Int] = [1 , 4 x 4 * 2 , 3] // [1 , 8, 8, 8, 8, 3]
func build() -> [some x some Type]

My biggest grip with x is that it really just looks and feels like multiplication. Even if I got past that -- which we all can, we're all smart-- it feels wrong here [6, 3 x 4, 2]. That looks absolutely like multiplication. This is more difficult to mentally parse in [1 , 4 * 2 x 4, 3]. Some may wonder if someone would even do this, regardless, it is possible. It's hard to see that 4 * 2 and 3 are separated by x.

n T

var a: [10 Int] and a = [10 10]
var a: (5 Int) for var a: (Int, Int, Int, Int, Int)
a = (5 2) for a = (2,2,2,2,2)
var x: [5 Int] = [6, 3 3, 2]
var x: [6 Int] = [1 , 4 4 * 2, 3] // [1 , 8, 8, 8, 8, 3]
func build() -> [some some Type]

This falls apart when we try to re-use for [10 10] or [some some Type]. I don't think that is acceptable.

n ; T

var a: [10; Int] and a = [10 10]
var a: (5; Int) for var a: (Int, Int, Int, Int, Int)
a = (5; 2) for a = (2,2,2,2,2)
var x: [5; Int] = [6, 3 ; 3, 2]
var x: [6 ; Int] = [1 , 4 ; 4 * 2, 3] // [1 , 8, 8, 8, 8, 3]
func build() -> [some ; some Type]

This one looks OK for the declaration [10; Int] but feels wrong for [some ; some Type] It looks the most unnatural. This doesn't seem to fit Swift as well. ; is an expression and statement delimiter. I know we can have a special meaning within [ ]and ( ) but that seems awkward. I also want to note that this is the reversed to Rust (I believe).

2 Likes