ints.choice vs. chars.choice

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 18 18:24:40 UTC 2019


On 11/18/19 12:32 PM, mipri wrote:
> Howdy,
> 
> The following program fails to compile if the second line
> is uncommented:
> 
> import std;
> 
> void main() {
>      writeln([1, 2, 3].choice);
>      //writeln(['a', 'b', 'c'].choice);
> }
> 
> Error: template std.random.choice cannot deduce function from argument 
> types !()(char[], MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 
> 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 4022730752u, 
> 18LU, 1812433253u)), candidates are:
> /usr/include/dmd/phobos/std/random.d(2559): std.random.choice(Range, 
> RandomGen = Random)(auto ref Range range, ref RandomGen urng) if 
> (isRandomAccessRange!Range && hasLength!Range && isUniformRNG!RandomGen)
> /usr/include/dmd/phobos/std/random.d(2569): 
> std.random.choice(Range)(auto ref Range range)
> 
> What is going on here? I get it that choice() isn't simply an algorithm
> over T[], that there are some additional constraints, but surely a
> char[] is just as random acc...

Nope, phobos treats a narrow character array (such as char[] or wchar[]) 
as a bidirectional range of dchar. It's called autodecoding, and it's 
continually causing problems for about 10 years now.

> ....
> 
> Oh. It's because of emojicode.

unicode. I hope that was a joke ;)

> This works:
> 
> import std;
> 
> void main() {
>      writeln([1, 2, 3].choice);
>      writeln(cast(char)(cast(uint8_t[])['a', 'b', 'c']).choice);

You could also use cast(dchar[]), and avoid the cast back to char.

-Steve


More information about the Digitalmars-d-learn mailing list