ints.choice vs. chars.choice
mipri
mipri at minimaltype.com
Mon Nov 18 17:32:32 UTC 2019
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...
...
Oh. It's because of emojicode.
This works:
import std;
void main() {
writeln([1, 2, 3].choice);
writeln(cast(char)(cast(uint8_t[])['a', 'b', 'c']).choice);
}
and this outputs false: writeln(isRandomAccessRange!(char[]));
I no longer have any questions but I wish the dlang.org docs were
much more generous with examples.
More information about the Digitalmars-d-learn
mailing list