Phobos' std.conv.text functions

via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 31 08:15:07 PDT 2015


I have been bitten by the std.conv.text family of functions twice 
now and while it's not a major issue, I think it still an issue 
that is worth fixing and perhaps prevent similar issues in the 
future.

First time I got bitten was because of local imports shadowing 
local symbols without error or at least a warning.

auto stuff(string text) {
   import std.conv;

   foreach(ch; text.byDchar) {
   }
}

Nowadays I always do selective imports, but sometimes for longer 
functions, this is sort of impractical. I think it's very 
error-prone for imports to override local symbols, but that's 
another issue.

Second time was similar but related to code morphing. I was 
developing some code that had a local variable called dtext. I 
later removed that symbol, but because there is a dtext function, 
it basically compiled fine. The problem took longer to find 
because I didn't know such family of functions even existed, so I 
was scratching my head lice for a while...

auto stuff(string x) {
   //auto dtext = x.toUTF32; // this got removed

   foreach(ch; dtext) { // forgot to update this line, and still 
compiled.
   }
}

It seems a little problematic IMO that this is a valid call, when 
called with no arguments. With UFCS, it is easy for functions 
with no arguments to impersonate variables in this way. To 
somewhat reduce the troubles, and given what these functions do, 
would adding a static args.length > 0 constraint to this and 
similar functions be an easy and acceptable fix?


More information about the Digitalmars-d mailing list