Fixed-size arrays and randomShuffle()

Ali Çehreli acehreli at yahoo.com
Thu May 3 06:34:43 PDT 2012


On 05/03/2012 06:30 AM, Vidar Wahlberg wrote:
> May be that this works as intended, but it fooled me:
> ---
> import std.random;
> import std.stdio;
> void main() {
> int[5] a = 0;
> a[0] = 1;
> int[] b = [1, 0, 0, 0, 0];
> randomShuffle(a);

Fixed-length arrays are value types. 'a' is copied to randomShuffle() so 
its copy is shuffled. Passing a slice of the whole array works:

         randomShuffle(a[]);

> writeln(a);
> randomShuffle(b);
> writeln(b);
> }
> ---
>
> In DMD 2.0.59 the fixed-size array "a" won't be shuffled (the dynamic
> array "b" will), and you won't get any warning about it.
>
> I'm not sure whether this counts as something that should be reported as
> a bug/improvement, nor if only randomShuffle() displays this behaviour,
> perhaps you could enlighten me.

Ali


More information about the Digitalmars-d-learn mailing list