Example from d-idioms is incorrect

Namespace via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 30 14:39:37 PDT 2015


On Thursday, 30 April 2015 at 21:30:36 UTC, TheGag96 wrote:
> I was looking at the d-idioms website today and saw this code 
> example:
>
> http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays
>
> And I was kind of irked. I just recently working with removing 
> an element from an array in a small project I worked on two 
> weeks ago, and I had to learn the hard way that to properly 
> remove an element from an array in the way this example 
> describes, you have to do array.length--; as well.
>
> This code:
>
> import std.stdio, std.algorithm;
>
> void main() {
>   int[] arr;  //ensuring it's dynamic
>   arr ~= 1;
>   arr ~= 5;
>   arr ~= 10;
>   arr.remove(1);
>   writeln(arr);
>   writeln(arr == [1, 10]);
>   arr.length--;
>   writeln(arr);
>   writeln(arr == [1, 10]);
> }
>
> produces this output:
>
> [1, 10, 10]
> false
> [1, 10]
> true
>
> Compiled and ran on Windows, dmd v2.067.0. Unless I'm totally 
> missing something here, that website is giving some pretty 
> wrong information... Was the behavior of the remove() function 
> changed recently? Thanks guys.

http://dpaste.dzfl.pl/007a9319371d

Application output:
[1, 10]
true
[1]
false


More information about the Digitalmars-d-learn mailing list