randomSample does not accept char[]
Timon Gehr
timon.gehr at gmx.ch
Tue Jan 22 02:54:49 PST 2013
On 01/22/2013 11:42 AM, andrea9940 wrote:
> Hi everyone,
> can you explain me why this code DOES compile:
>
> /*-------------------------------------------------------*/
> import std.range, std.random;
> void main()
> {
> int[] vowels = ['A', 'E', 'I', 'O', 'U'];
> static assert(isInputRange!(typeof(vowels)));
> auto x = randomSample(vowels, 1);
> }
> /*-------------------------------------------------------*/
>
> but this DOES NOT:
>
> /*-------------------------------------------------------*/
> import std.range, std.random;
> void main()
> {
> char[] vowels = ['A', 'E', 'I', 'O', 'U'];
> static assert(isInputRange!(typeof(vowels))); //pass
> auto x = randomSample(vowels, 1); //fail
> }
> /*-------------------------------------------------------*/
> main.d(6): Error: template std.random.randomSample(R) if
> (isInputRange!(R)) does not match any function template declaration
> main.d(6): Error: template std.random.randomSample(R) if
> (isInputRange!(R)) cannot deduce template function from argument types
> !()(char[],int)
Use randomSample(vowels, 1, vowels.length); to make it work.
The error message is bad.
You are trying to call the following overload:
auto randomSample(R)(R r, size_t n)
if(isInputRange!R && hasLength!R)
{
return RandomSample!(R, void)(r, n, r.length);
}
hasLength!(char[]) is false. This is because Phobos considers char[],
const(char)[] and immutable(char)[] ranges of dchar.
More information about the Digitalmars-d
mailing list