www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - map!(char)(string) problem

reply David Held <dmd wyntrmute.com> writes:
import std.algorithm;

int toInt(char c) { return 1; }

void main()
{
     map!(a => toInt(a))("hello");
}

Can someone please explain why I get this:

Bug.d(10): Error: function Bug.toInt (char c) is not callable using 
argument types (dchar)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
D:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(425): Error: 
template instance Bug.main.__lambda1!dchar error instantiating
D:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(411): 
instantiated from here: MapResult!(__lambda1, string)
Bug.d(10):        instantiated from here: map!string
D:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(411): Error: 
template instance Bug.main.MapResult!(__lambda1, string) error instantiating
Bug.d(10):        instantiated from here: map!string
Bug.d(10): Error: template instance Bug.main.map!((a) => 
toInt(a)).map!string error instantiating

I thought that string == immutable char[], but this implies that it is 
getting inferred as dchar[], I guess.

Dave
May 03 2014
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Sat, 03 May 2014 14:47:56 -0700
David Held via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 import std.algorithm;
 
 int toInt(char c) { return 1; }
 
 void main()
 {
      map!(a => toInt(a))("hello");
 }
 
 Can someone please explain why I get this:
 
 Bug.d(10): Error: function Bug.toInt (char c) is not callable using 
 argument types (dchar)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 D:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(425): Error: 
 template instance Bug.main.__lambda1!dchar error instantiating
 D:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(411): 
 instantiated from here: MapResult!(__lambda1, string)
 Bug.d(10):        instantiated from here: map!string
 D:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(411): Error: 
 template instance Bug.main.MapResult!(__lambda1, string) error
 instantiating Bug.d(10):        instantiated from here: map!string
 Bug.d(10): Error: template instance Bug.main.map!((a) => 
 toInt(a)).map!string error instantiating
 
 I thought that string == immutable char[], but this implies that it
 is getting inferred as dchar[], I guess.
All strings are treated as ranges of dchar by Phobos. http://stackoverflow.com/questions/12288465 If you really want to operate on strings as ranges of code units rather than code points, then you need to use std.string.representation and convert them to the equivalent integral types (e.g. immutable(ubyte)[]). - Jonathan M Davis
May 03 2014