Auto-casting in range based functions?
    Artur Skawina 
    art.08.09 at gmail.com
       
    Sun May 13 11:31:25 PDT 2012
    
    
  
On 05/13/12 19:49, Andrew Stanton wrote:
> I have been playing around with D as a scripting tool and have been running into the following issue:
> 
> -----------------------------------
> import std.algorithm;
> 
> struct Delim {
>     char delim;
>     this(char d) {
>         delim = d;
>     }
> }
> 
> void main() {
>     char[] d = ['a', 'b', 'c'];
>     auto delims = map!Delim(d);
> }
> 
> /*
> Compiling gives me the following error:
> /usr/include/d/dmd/phobos/std/algorithm.d(382): Error: constructor test.Delim.this (char d) is not callable using argument types (dchar)
> /usr/include/d/dmd/phobos/std/algorithm.d(382): Error: cannot implicitly convert expression ('\U0000ffff') of type dchar to char
> 
> */
> 
> -----------------------------------
> 
> As someone who most of the time doesn't need to handle unicode, is there a way I can convince these functions to not upcast char to dchar?  I can't think of a way to make the code more explicit in its typing.
Well, if you don't want/need utf8 at all:
   alias ubyte ascii;
   int main() {
       ascii[] d = ['a', 'b', 'c'];
       auto delims = map!Delim(d);
       //...
and if you want to avoid utf8 just for this case (ie you "know" 'd[]'
contains just ascii) something like this should work:
    char[] d = ['a', 'b', 'c'];
    auto delims = map!((c){assert(c<128); return Delim(cast(char)c);})(d);
(it's probably more efficient when written as
    auto delims = map!Delim(cast(ascii[])d);
but you loose the safety checks)
artur
    
    
More information about the Digitalmars-d-learn
mailing list