www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Detect the bug in the following code

reply "Idan Arye" <GenericNPC gmail.com> writes:
import std.stdio;

struct Foo {
     bool registered = false;

     void register(int x) {
         writeln("Registering ", x);
         register = true;
     }
}

void main() {
     Foo foo;
     foo.register(10);
}
Apr 15 2015
next sibling parent "weaselcat" <weaselcat gmail.com> writes:
On Wednesday, 15 April 2015 at 14:44:48 UTC, Idan Arye wrote:
 import std.stdio;

 struct Foo {
     bool registered = false;

     void register(int x) {
         writeln("Registering ", x);
         register = true;
     }
 }

 void main() {
     Foo foo;
     foo.register(10);
 }
register = true
Apr 15 2015
prev sibling next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 15 April 2015 at 14:44:48 UTC, Idan Arye wrote:
         register = true;
Easy, you assigned to the wrong variable, that'd quickly be obvious at runtime too with a stack overflow. I think it sucks that true and false will implicitly convert to int though, that bites people somewhat often. I wonder how much annoyance it would be to remove that implicit conversion.
Apr 15 2015
parent "Idan Arye" <GenericNPC gmail.com> writes:
On Wednesday, 15 April 2015 at 14:46:56 UTC, Adam D. Ruppe wrote:
 On Wednesday, 15 April 2015 at 14:44:48 UTC, Idan Arye wrote:
        register = true;
Easy, you assigned to the wrong variable, that'd quickly be obvious at runtime too with a stack overflow. I think it sucks that true and false will implicitly convert to int though, that bites people somewhat often. I wonder how much annoyance it would be to remove that implicit conversion.
Yea, in this example it's quite obvious, but I've just before posting it I realized I did this mistake in a much larger project, and it was harder to detect(I was getting a double registration error in another module rather than a stack overflow). At any rate, I don't think the problem is the implicit conversion as much as it is the property syntax...
Apr 15 2015
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/15/15 10:44 AM, Idan Arye wrote:
 import std.stdio;

 struct Foo {
      bool registered = false;

      void register(int x) {
          writeln("Registering ", x);
          register = true;
      }
 }

 void main() {
      Foo foo;
      foo.register(10);
 }
Easy, the bug is in DMD improperly accepting property assignment without property annotation :P -Steve
Apr 15 2015
next sibling parent "Brian Schott" <briancschott gmail.com> writes:
On Wednesday, 15 April 2015 at 15:17:32 UTC, Steven Schveighoffer 
wrote:
 Easy, the bug is in DMD improperly accepting property 
 assignment without  property annotation :P
We've found the winner!
Apr 15 2015
prev sibling next sibling parent reply "w0rp" <devw0rp gmail.com> writes:
On Wednesday, 15 April 2015 at 15:17:32 UTC, Steven Schveighoffer 
wrote:
 On 4/15/15 10:44 AM, Idan Arye wrote:
 import std.stdio;

 struct Foo {
     bool registered = false;

     void register(int x) {
         writeln("Registering ", x);
         register = true;
     }
 }

 void main() {
     Foo foo;
     foo.register(10);
 }
Easy, the bug is in DMD improperly accepting property assignment without property annotation :P -Steve
Yep. Push patches for DIP23. Get it in the compiler already. Make only x.foo legal without property.
Apr 15 2015
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 15 April 2015 at 18:25:33 UTC, w0rp wrote:
 On Wednesday, 15 April 2015 at 15:17:32 UTC, Steven 
 Schveighoffer wrote:
 On 4/15/15 10:44 AM, Idan Arye wrote:
 import std.stdio;

 struct Foo {
    bool registered = false;

    void register(int x) {
        writeln("Registering ", x);
        register = true;
    }
 }

 void main() {
    Foo foo;
    foo.register(10);
 }
Easy, the bug is in DMD improperly accepting property assignment without property annotation :P -Steve
Yep. Push patches for DIP23. Get it in the compiler already. Make only x.foo legal without property.
See http://forum.dlang.org/post/kesbfurmakedjqcqljzg forum.dlang.org
Apr 15 2015
prev sibling parent reply "ixid" <adamsibson hotmail.com> writes:
On Wednesday, 15 April 2015 at 15:17:32 UTC, Steven Schveighoffer 
wrote:
 On 4/15/15 10:44 AM, Idan Arye wrote:
 import std.stdio;

 struct Foo {
     bool registered = false;

     void register(int x) {
         writeln("Registering ", x);
         register = true;
     }
 }

 void main() {
     Foo foo;
     foo.register(10);
 }
Easy, the bug is in DMD improperly accepting property assignment without property annotation :P -Steve
writeln = "Now this is abused of the syntax."
Apr 16 2015
parent reply "deadalnix" <deadalnix gmail.com> writes:
On Thursday, 16 April 2015 at 13:52:32 UTC, ixid wrote:
 writeln = "Now this is abused of the syntax."
The important question is, can you assign a tuple and get the auto unpacking to kick in ?
Apr 16 2015
parent reply "Idan Arye" <GenericNPC gmail.com> writes:
On Thursday, 16 April 2015 at 21:37:39 UTC, deadalnix wrote:
 On Thursday, 16 April 2015 at 13:52:32 UTC, ixid wrote:
 writeln = "Now this is abused of the syntax."
The important question is, can you assign a tuple and get the auto unpacking to kick in ?
"%s %s".writefln = ("foo".tuple = "bar").expand;
Apr 16 2015
parent "Brian Schott" <briancschott gmail.com> writes:
On Thursday, 16 April 2015 at 22:48:53 UTC, Idan Arye wrote:
 "%s %s".writefln = ("foo".tuple = "bar").expand;
Please make a pull request: https://github.com/Hackerpilot/Idiotmatic-D
Apr 16 2015
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 15 April 2015 at 14:44:48 UTC, Idan Arye wrote:
 import std.stdio;

 struct Foo {
     bool registered = false;

     void register(int x) {
         writeln("Registering ", x);
         register = true;
     }
 }

 void main() {
     Foo foo;
     foo.register(10);
 }
Property assignment syntax for non-property functions is a horrible, horrible thing.
Apr 15 2015
parent reply "Caspar" <Caspar Kielwein.de> writes:
On Wednesday, 15 April 2015 at 17:28:01 UTC, John Colvin wrote:
 On Wednesday, 15 April 2015 at 14:44:48 UTC, Idan Arye wrote:
 import std.stdio;

 struct Foo {
    bool registered = false;

    void register(int x) {
        writeln("Registering ", x);
        register = true;
    }
 }

 void main() {
    Foo foo;
    foo.register(10);
 }
Property assignment syntax for non-property functions is a horrible, horrible thing.
Could someone please explain what is actually happening in that piece of code to a D newbie? I see what happens when I run the code but I don't get why it is happening. In particular what "register = true;" actually does in that case. Thanks, Caspar
Apr 15 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/15/15 1:44 PM, Caspar wrote:
 On Wednesday, 15 April 2015 at 17:28:01 UTC, John Colvin wrote:
 On Wednesday, 15 April 2015 at 14:44:48 UTC, Idan Arye wrote:
 import std.stdio;

 struct Foo {
    bool registered = false;

    void register(int x) {
        writeln("Registering ", x);
        register = true;
    }
 }

 void main() {
    Foo foo;
    foo.register(10);
 }
Property assignment syntax for non-property functions is a horrible, horrible thing.
Could someone please explain what is actually happening in that piece of code to a D newbie? I see what happens when I run the code but I don't get why it is happening. In particular what "register = true;" actually does in that case.
In D1, and in D2 (but we're trying to change it), assignment properties worked like this: struct S { void foo(int x); } S s; s.foo = 1; // same as s.foo(1); So what is happening is that: register = true; translates to: register(true); which then matches the call of register(int) because 'true' is treated as 1. So while the caller really wanted to set the variable "registered" to true, he's recursively calling his own function. Many of us think that this should not work, because we have a way to designate properties via: property void foo(int x); So really only functions tagged with property should be usable as setters. Getters, the distinction is less confusing, because the following aren't very different: auto x = foo; auto x = foo(); So the position myself and many of us here have is that normal functions can be used as getters, but only property tagged functions should be usable as setters. -Steve
Apr 15 2015