digitalmars.D - Detect the bug in the following code
- Idan Arye (12/12) Apr 15 2015 import std.stdio;
- weaselcat (2/14) Apr 15 2015 register = true
- Adam D. Ruppe (6/7) Apr 15 2015 Easy, you assigned to the wrong variable, that'd quickly be
- Idan Arye (8/15) Apr 15 2015 Yea, in this example it's quite obvious, but I've just before
- Steven Schveighoffer (4/16) Apr 15 2015 Easy, the bug is in DMD improperly accepting property assignment without...
- Brian Schott (3/5) Apr 15 2015 We've found the winner!
- w0rp (4/23) Apr 15 2015 Yep. Push patches for DIP23. Get it in the compiler already. Make
- John Colvin (3/28) Apr 15 2015 See
- ixid (3/22) Apr 16 2015 writeln = "Now this is abused of the syntax."
- deadalnix (3/4) Apr 16 2015 The important question is, can you assign a tuple and get the
- Idan Arye (2/6) Apr 16 2015 "%s %s".writefln = ("foo".tuple = "bar").expand;
- Brian Schott (3/4) Apr 16 2015 Please make a pull request:
- John Colvin (3/15) Apr 15 2015 Property assignment syntax for non-property functions is a
- Caspar (8/26) Apr 15 2015 Could someone please explain what is actually happening in that
- Steven Schveighoffer (28/52) Apr 15 2015 In D1, and in D2 (but we're trying to change it), assignment properties
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
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
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
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: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...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
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
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 :PWe've found the winner!
Apr 15 2015
On Wednesday, 15 April 2015 at 15:17:32 UTC, Steven Schveighoffer wrote:On 4/15/15 10:44 AM, Idan Arye wrote:Yep. Push patches for DIP23. Get it in the compiler already. Make only x.foo legal without property.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
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:See http://forum.dlang.org/post/kesbfurmakedjqcqljzg forum.dlang.orgOn 4/15/15 10:44 AM, Idan Arye wrote:Yep. Push patches for DIP23. Get it in the compiler already. Make only x.foo legal without property.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
On Wednesday, 15 April 2015 at 15:17:32 UTC, Steven Schveighoffer wrote:On 4/15/15 10:44 AM, Idan Arye wrote:writeln = "Now this is abused of the syntax."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 16 2015
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
On Thursday, 16 April 2015 at 21:37:39 UTC, deadalnix wrote:On Thursday, 16 April 2015 at 13:52:32 UTC, ixid wrote:"%s %s".writefln = ("foo".tuple = "bar").expand;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
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
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
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: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, Casparimport 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
On 4/15/15 1:44 PM, Caspar wrote:On Wednesday, 15 April 2015 at 17:28:01 UTC, John Colvin wrote: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. -SteveOn Wednesday, 15 April 2015 at 14:44:48 UTC, Idan Arye wrote: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.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