View mode: basic / threaded / horizontal-split · Log in · Help
January 22, 2013
randomSample does not accept char[]
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)
January 22, 2013
Re: randomSample does not accept char[]
Short answer: unicode
Long answer is here: 
http://dlang.org/phobos/std_range.html#.hasLength , please pay 
attention to narrow string paragraph.
(failed constraint is hasLength!char[])

int is >= dchar so no issues for int.
January 22, 2013
Re: randomSample does not accept char[]
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.
January 22, 2013
Re: randomSample does not accept char[]
On Tuesday, 22 January 2013 at 10:53:16 UTC, mist wrote:
> Short answer: unicode
> Long answer is here: 
> http://dlang.org/phobos/std_range.html#.hasLength , please pay 
> attention to narrow string paragraph.
> (failed constraint is hasLength!char[])
>
> int is >= dchar so no issues for int.

Thanks !
January 22, 2013
Re: randomSample does not accept char[]
This is a good hint, by the way, that something needs to be done 
about constraint error messages or we are risk to fallback to C++ 
world of pretty template library errors. I usually just go 
straight to template sources to save some time but that is hardly 
expected from a newcomer.
Top | Discussion index | About this forum | D home