Swift deprecate i++ and c-style for loop

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 24 10:06:19 PST 2016


On 2/24/16 11:40 AM, Meta wrote:
> On Wednesday, 24 February 2016 at 16:37:04 UTC, Suliman wrote:
>> https://github.com/apple/swift/blob/master/CHANGELOG.md
>>
>> func slices() {
>>     var array = ["First", "Second", "Third", "Fourth"]
>>     array.removeLast()
>>     array.removeFirst()
>> }
>>
>> also look very intuitive. I looked at std.algorithm.mutation and did
>> not find anything for this. I see only simple remove option.
>
> The D equivalent is this:
>
> array = array[0..$ - 1];
> array = array[1..$];
>
> Arguably just as intuitive.

I think there is a difference -- Swift arrays are not slices, so Array 
is not the same type as ArraySlice.

I believe Array.removeFirst is O(n). I think it also returns the element 
removed.

I also believe that it will affect any slices of the array, unlike D code.

Swift also has Array.dropFirst(n), which does what the D slice does (and 
returns an ArraySlice). Arrays also support slicing like D does, though 
with different index range notation.

Not 100% sure, the docs are not super clear on this.

I'm quite glad D stuck with the same type for arrays and array slices.

-Steve


More information about the Digitalmars-d mailing list