www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - randomSample does not accept char[]

reply "andrea9940" <no mail.plz> writes:
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)
Jan 22 2013
next sibling parent "mist" <none none.none> writes:
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.
Jan 22 2013
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Jan 22 2013
prev sibling next sibling parent "andrea9940" <no mail.plz> writes:
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 !
Jan 22 2013
prev sibling parent "mist" <none none.none> writes:
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.
Jan 22 2013