www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Phobos' std.conv.text functions

reply =?UTF-8?B?Ik3DoXJjaW8=?= Martins" <marcioapm gmail.com> writes:
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?
Aug 31 2015
parent reply "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Mon, Aug 31, 2015 at 03:15:07PM +0000, via Digitalmars-d wrote:
 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) {
   }
 }
https://issues.dlang.org/show_bug.cgi?id=10378 Kenji has a pull for this, let's hope it goes through. T -- English is useful because it is a mess. Since English is a mess, it maps well onto the problem space, which is also a mess, which we call reality. Similarly, Perl was designed to be a mess, though in the nicest of all possible ways. -- Larry Wall
Aug 31 2015
next sibling parent =?UTF-8?B?Ik3DoXJjaW8=?= Martins" <marcioapm gmail.com> writes:
On Monday, 31 August 2015 at 15:39:10 UTC, H. S. Teoh wrote:
 On Mon, Aug 31, 2015 at 03:15:07PM +0000, via Digitalmars-d 
 wrote:
 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) {
   }
 }
https://issues.dlang.org/show_bug.cgi?id=10378 Kenji has a pull for this, let's hope it goes through. T
Great! This PR seems to only fix the local import shadowing bug, though.
Aug 31 2015
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 08/31/2015 05:35 PM, H. S. Teoh via Digitalmars-d wrote:
 On Mon, Aug 31, 2015 at 03:15:07PM +0000, via Digitalmars-d wrote:
 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) {
    }
 }
https://issues.dlang.org/show_bug.cgi?id=10378 Kenji has a pull for this, let's hope it goes through. T
Of course it is better than the status quo, but if it does, the bug report should stay open, and/or a few new ones should be filed. The pull ignores the discussion about what would be a good design and implements the rather awkward semantics that are quickest to hack into the compiler instead. Also, the proposed rewrite does not fix the problem. (I haven't tested it, but AFAIU, the following code is neither rejected by the pull, nor does it print "123".) import std.stdio; void main(){ string text="123"; void foo(){ import std.conv; writeln(text()); } foo(); } (Also, essentially the same logic occurs in three distinct places in the new code.)
Aug 31 2015
parent reply "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Mon, Aug 31, 2015 at 07:40:24PM +0200, Timon Gehr via Digitalmars-d wrote:
 On 08/31/2015 05:35 PM, H. S. Teoh via Digitalmars-d wrote:
[...]
 Also, the proposed rewrite does not fix the problem. (I haven't tested
 it, but AFAIU, the following code is neither rejected by the pull, nor
 does it print "123".)
 
 import std.stdio;
 
 void main(){
     string text="123";
     void foo(){
         import std.conv;
         writeln(text());
     }
     foo();
 }
 
 (Also, essentially the same logic occurs in three distinct places in
 the new code.)
This code should *not* print "123", because text() is explicitly a function call, but the local variable 'text' is not a function. T -- Windows: the ultimate triumph of marketing over technology. -- Adrian von Bidder
Aug 31 2015
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/31/2015 07:45 PM, H. S. Teoh via Digitalmars-d wrote:
 On Mon, Aug 31, 2015 at 07:40:24PM +0200, Timon Gehr via Digitalmars-d wrote:
 On 08/31/2015 05:35 PM, H. S. Teoh via Digitalmars-d wrote:
[...]
 Also, the proposed rewrite does not fix the problem. (I haven't tested
 it, but AFAIU, the following code is neither rejected by the pull, nor
 does it print "123".)

 import std.stdio;

 void main(){
      string text="123";
      void foo(){
          import std.conv;
          writeln(text());
      }
      foo();
 }

 (Also, essentially the same logic occurs in three distinct places in
 the new code.)
This code should *not* print "123", because text() is explicitly a function call, but the local variable 'text' is not a function. ...
It's a typo, presumably caused by implicit good intentions. :o) What I meant to write was this: import std.stdio; void main(){ string text="123"; void foo(){ import std.conv; writeln(text); } foo(); }
Aug 31 2015