<< Operator to append to array

I already have an implementation:

func <<<T>(var left: Array<T>, right:T)

{

    left.append(right)

}

It makes

myArray.append(myArrayItem)

become:

myArray << myArrayItem

Which is shorter and a lot of languages such as Ruby already use this.

···

--
 Wizard
james@supmenow.com
+44 7523 279 698

You can do this already

left += [right]

···

On Dec 21, 2015, at 9:22 AM, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:

I already have an implementation:

func <<<T>(var left: Array<T>, right:T)

{

    left.append(right)

}

It makes

myArray.append(myArrayItem)

become:

myArray << myArrayItem

Which is shorter and a lot of languages such as Ruby already use this.

--
 Wizard
james@supmenow.com <mailto:james@supmenow.com>
+44 7523 279 698
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I should have said

myArray += [myArrayItem]

···

On Dec 21, 2015, at 9:28 AM, Paul Ossenbruggen <possen@gmail.com> wrote:

You can do this already

left += [right]

On Dec 21, 2015, at 9:22 AM, James Campbell via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

I already have an implementation:

func <<<T>(var left: Array<T>, right:T)

{

    left.append(right)

}

It makes

myArray.append(myArrayItem)

become:

myArray << myArrayItem

Which is shorter and a lot of languages such as Ruby already use this.

--
 Wizard
james@supmenow.com <mailto:james@supmenow.com>
+44 7523 279 698
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

Ohh didn't know that :)

···

On Mon, Dec 21, 2015 at 5:30 PM, Paul Ossenbruggen <possen@gmail.com> wrote:

I should have said

myArray += [myArrayItem]

On Dec 21, 2015, at 9:28 AM, Paul Ossenbruggen <possen@gmail.com> wrote:

You can do this already

left += [right]

On Dec 21, 2015, at 9:22 AM, James Campbell via swift-evolution < > swift-evolution@swift.org> wrote:

I already have an implementation:

func <<<T>(var left: Array<T>, right:T)

{

    left.append(right)

}

It makes

myArray.append(myArrayItem)

become:

myArray << myArrayItem

Which is shorter and a lot of languages such as Ruby already use this.

--
 Wizard
james@supmenow.com
+44 7523 279 698
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
 Wizard
james@supmenow.com
+44 7523 279 698

can we add

extension Array {
    @inlinable public static func += (array: inout [Element], element: Element) {
        array.append(element)
    }
}

to Collection.Array.swift?

2 Likes

Please don’t resurrect old threads this way. If you’d like to discuss the topic again, you can start a new thread and summarize the previous discussion for your readers.

To answer your question: This would likely significantly impact type checking performance for arithmetic types. It would also cause confusion to readers where the element is itself an Array. Finally, we don’t offer duplicative ways to spell the same method, and this one is already called append.

(In general, syntactic sugar has historically been the lowest priority for Swift Evolution. They also have some of the highest bars for acceptance and elicit the most bikeshedding. Probably best avoided altogether.)

3 Likes

Just to quickly add (though we should really start a new thread), that it also leads to ambiguity when used with array of Any. Since you don't know if you're appending a collection as element or as a list of elements. That's why append comes in two flavors; append(_:) for single element and append(contentsOf:) for list of elements.

Isn't that pretty much the definition of an operator? :wink:

7 Likes

I'd expect the compiler to complain in that case

From memory, there’s also an optimisation technique in the compiler to turn a short array literal appended with += [item] to append methods per item to avoid the array allocation altogether anyway. I’m not sure what benefit this idea has over the existing behavior.

For sure, it'd be compile-time error. The problem is that it easily leads to slow type checker, which leads to slow compilation time, IDE suggestion, etc.

Hmm, I thought we use CollectionOfOne for that.

You can, but in practice most don’t for readability. It makes sense to optimize it anyway.

Also note I said “short”, not just singular. I believe with around 3-4 items or less in the array, it will optimize it.

See the following PR where this was implemented:

The limit is actually 6 or less in the code, consistent with the following comment from an earlier PR: SILOptimizer: Replace [].append(contentsOf:) with [].append(element:) by ben-ng · Pull Request #6652 · apple/swift · GitHub

2 Likes