www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - map on char[] converts to dchar?

reply Johan <j j.nl> writes:
Hi all,
   What am I doing wrong here?
```
import std.algorithm;
int foo(char c) {
     return 123;
}
auto mapFoo(char[] chars) {
     return chars.map!(a => a.foo);
}
```
errors with:
main.d(14): Error: function main.foo (char c) is not callable 
using argument types (dchar)
/Library/D/dmd/src/phobos/std/algorithm/iteration.d(492):        
instantiated from here: MapResult!(__lambda2, char[])
main.d(14):        instantiated from here: map!(char[])

(Things work after changing foo to accept dchar, but I don't want 
that.)

Thanks,
   Johan
Jul 24
parent reply Seb <seb wilzba.ch> writes:
On Monday, 24 July 2017 at 09:05:43 UTC, Johan wrote:
 Hi all,
   What am I doing wrong here?
 ```
 import std.algorithm;
 int foo(char c) {
     return 123;
 }
 auto mapFoo(char[] chars) {
     return chars.map!(a => a.foo);
 }
 ```
 errors with:
 main.d(14): Error: function main.foo (char c) is not callable 
 using argument types (dchar)
 /Library/D/dmd/src/phobos/std/algorithm/iteration.d(492):
  instantiated from here: MapResult!(__lambda2, char[])
 main.d(14):        instantiated from here: map!(char[])

 (Things work after changing foo to accept dchar, but I don't 
 want that.)

 Thanks,
   Johan
Have a look at https://tour.dlang.org/tour/en/gems/unicode tl;dr: it's auto-decoding and you can avoid it with e.g. byChar
Jul 24
parent pineapple <meapineapple gmail.com> writes:
It is worth noting too that mach's map function will not behave 
this way; UTF encoding and decoding is instructed explicitly and 
is not done implicitly like in phobos.

https://github.com/pineapplemachine/mach.d

     import mach.range : map, asarray;
     import mach.text.ascii : toupper;

     void main(){
         char[] x = ['h', 'e', 'l', 'l', 'o'];
         char[] y = x.map!toupper.asarray();
         assert(y == "HELLO");
     }
Jul 24