| Thread overview | |||||
|---|---|---|---|---|---|
|
April 08, 2015 why does phobos use [0, 5, 8, 9][] instead of [0, 5, 8, 9] in examples? | ||||
|---|---|---|---|---|
| ||||
Attachments:
| Eg, code like this in std.algorithm: assert(equal(setSymmetricDifference(a, b), [0, 5, 8, 9][])); why not just: assert(equal(setSymmetricDifference(a, b), [0, 5, 8, 9])); ? | |||
April 08, 2015 Re: why does phobos use [0, 5, 8, 9][] instead of [0, 5, 8, 9] in examples? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | On Wednesday, 8 April 2015 at 02:40:14 UTC, Timothee Cour wrote:
> Eg, code like this in std.algorithm:
> assert(equal(setSymmetricDifference(a, b), [0, 5, 8, 9][]));
> why not just:
> assert(equal(setSymmetricDifference(a, b), [0, 5, 8, 9]));
> ?
It's historic. DMD 2.041 changed the type of array literals from int[N] (i.e. fixed-length literals of static arrays) to int[] (dynamic arrays). Since [] means to take a slice of the entire static array, it would tell the compiler to create a static array, but only pass a slice of it to equal() instead of passing the entire static array by-value.
| |||
April 08, 2015 Re: why does phobos use [0, 5, 8, 9][] instead of [0, 5, 8, 9] in examples? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | On 04/07/2015 07:40 PM, Timothee Cour via Digitalmars-d-learn wrote:
> Eg, code like this in std.algorithm:
> assert(equal(setSymmetricDifference(a, b), [0, 5, 8, 9][]));
> why not just:
> assert(equal(setSymmetricDifference(a, b), [0, 5, 8, 9]));
> ?
>
It must be a leftover from the time when the type of an array literal was a static array (versus a dynamic one). Since static arrays are not ranges, the author of the code apparently has first created a slice to its elements.
However, today array literals are already slices.
Ali
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply