Problem with string.whitespace and newline

2011-06-11 Thread Michael Chen
The following code cannot be compiled
string clean(string x)
{
return join(split(x),whitespace);
}

The compile error is
Error   2   Error: template std.array.join(RoR,R) if (isInputRange!(RoR)
 isInputRange!(ElementType!(RoR))  isForwardRange!(R)) cannot
deduce template function from argument types
!()(string[],immutable(char[6u]))

However this line is fine
 join(split(x), );


The same problem happens when using newline. Is this a bug?

Best,
Mike


Re: Problem with string.whitespace and newline

2011-06-11 Thread Andrej Mitrovic
try return join(split(x),whitespace[]);

It seems whitespace is a static array.

On 6/12/11, Michael Chen sth4...@gmail.com wrote:
 The following code cannot be compiled
 string clean(string x)
 {
   return join(split(x),whitespace);
 }

 The compile error is
 Error 2   Error: template std.array.join(RoR,R) if (isInputRange!(RoR)
  isInputRange!(ElementType!(RoR))  isForwardRange!(R)) cannot
 deduce template function from argument types
 !()(string[],immutable(char[6u]))

 However this line is fine
  join(split(x), );


 The same problem happens when using newline. Is this a bug?

 Best,
 Mike



Re: Problem with string.whitespace and newline

2011-06-11 Thread Michael Chen
Thanks Andrej, it works.

On Sun, Jun 12, 2011 at 9:14 AM, Andrej Mitrovic
andrej.mitrov...@gmail.com wrote:
 try return join(split(x),whitespace[]);

 It seems whitespace is a static array.

 On 6/12/11, Michael Chen sth4...@gmail.com wrote:
 The following code cannot be compiled
 string clean(string x)
 {
       return join(split(x),whitespace);
 }

 The compile error is
 Error 2       Error: template std.array.join(RoR,R) if (isInputRange!(RoR)
  isInputRange!(ElementType!(RoR))  isForwardRange!(R)) cannot
 deduce template function from argument types
 !()(string[],immutable(char[6u]))

 However this line is fine
  join(split(x), );


 The same problem happens when using newline. Is this a bug?

 Best,
 Mike




Re: Problem with string.whitespace and newline

2011-06-11 Thread Andrej Mitrovic
I've said it before, but I'll say it again: this is such a common
error that it needs a better error message. Something as simple as
this will do:
foo can't take bar of type char[16]. Try using a slice: bar[].


Re: Problem with string.whitespace and newline

2011-06-11 Thread Jonathan M Davis
On 2011-06-11 18:46, Andrej Mitrovic wrote:
 I've said it before, but I'll say it again: this is such a common
 error that it needs a better error message. Something as simple as
 this will do:
 foo can't take bar of type char[16]. Try using a slice: bar[].

Have you created an enhancement request for it? If not, it's not likely to 
happen.

Though honestly, this is a bit of a tough one, because all that the compiler 
really knows is that the argument that you gave to the template function 
didn't match any of that template function's overloads. It has no idea why. 
And maybe there's an overload which is _supposed_ to take a static array for 
some reason, and it's another argument to the function that you screwed up?

There's probably some sort of special-casing that the compiler could do when 
trying to instantiating a template function and one of the arguments it was 
given was a static array, but the compiler is generally designed to tell that 
what you did doesn't work rather than tell you what you're supposed to do. So, 
getting error messages to give suggestions (particularly when templates are 
involved) can be tricky. It's been done before with the spellchecker, but I 
don't know how straightforward it would be to do that sort of thing with 
static arrays given that it has a much broader scope.

Template errors are just generally bad. They're definitely better than in C++, 
but I still don't think that they're anywhere near good enough for a lot of 
the more average programmers. If you start messing with them a lot, then the 
template constraints are actually quite good at giving you a clue as to what's 
wrong, but for newbies in particular, they're pretty incomprehensible.

- Jonathan M Davis


Re: Problem with string.whitespace and newline

2011-06-11 Thread Andrej Mitrovic
I can't seem to find anything specific like an enhancement request.

Here's a similar report by you:
http://d.puremagic.com/issues/show_bug.cgi?id=4971

Maybe we should have a 'common (newbie) mistakes' section on the
website and add this bit of information there.


Re: Problem with string.whitespace and newline

2011-06-11 Thread Jonathan M Davis
On 2011-06-11 19:29, Andrej Mitrovic wrote:
 I can't seem to find anything specific like an enhancement request.
 
 Here's a similar report by you:
 http://d.puremagic.com/issues/show_bug.cgi?id=4971
 
 Maybe we should have a 'common (newbie) mistakes' section on the
 website and add this bit of information there.

It certainly sounds like a good idea. And thanks to the fact that we're 
switching to d-programming-language.org, and its source is up on github, it's 
even possible for anyone to make the changes necessary and create a pull 
request for it.

What I'd like to see templates be able to do though is better recognize a type 
that _will_ work in at least a few common cases. Static arrays are one (if all 
of the template overloads fail, perhaps it should try instiating them with the 
dynamic version of the same type). Another is immutable dynamic arrays. 
const(T)[] would work if you declared the template with those explicit 
parameters, but as long as it's taking R or Range instead of const(T)[] for 
arrays, a range-based function won't work with immutable arrays even though it 
should. But that sort of change could be too big a change to how templates 
work. I don't know. I should probably write up an enhancement request on the 
subject.

- Jonathan M Davis


Re: Problem with string.whitespace and newline

2011-06-11 Thread Jonathan M Davis
On 2011-06-11 19:41, Jonathan M Davis wrote:
 On 2011-06-11 19:29, Andrej Mitrovic wrote:
  I can't seem to find anything specific like an enhancement request.
  
  Here's a similar report by you:
  http://d.puremagic.com/issues/show_bug.cgi?id=4971
  
  Maybe we should have a 'common (newbie) mistakes' section on the
  website and add this bit of information there.
 
 It certainly sounds like a good idea. And thanks to the fact that we're
 switching to d-programming-language.org, and its source is up on github,
 it's even possible for anyone to make the changes necessary and create a
 pull request for it.
 
 What I'd like to see templates be able to do though is better recognize a
 type that _will_ work in at least a few common cases. Static arrays are
 one (if all of the template overloads fail, perhaps it should try
 instiating them with the dynamic version of the same type). Another is
 immutable dynamic arrays. const(T)[] would work if you declared the
 template with those explicit parameters, but as long as it's taking R or
 Range instead of const(T)[] for arrays, a range-based function won't work
 with immutable arrays even though it should. But that sort of change could
 be too big a change to how templates work. I don't know. I should probably
 write up an enhancement request on the subject.

http://d.puremagic.com/issues/show_bug.cgi?id=6148