www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Warning on self assignment

reply Arun Chandrasekaran <aruncxy gmail.com> writes:
So I was telling my colleague that D would warn on self 
assignment, but found that I was wrong.

https://run.dlang.io/is/HLhtek

```
module a;

import std.stdio;

void main() {
     string a;
     a = a;          // Can the compiler warn at this line that 
there is no effect?
     writeln(a);
     return;
}
```
Apr 24 2018
next sibling parent reply Mike Franklin <slavo5150 yahoo.com> writes:
On Wednesday, 25 April 2018 at 01:08:46 UTC, Arun Chandrasekaran 
wrote:
 So I was telling my colleague that D would warn on self 
 assignment, but found that I was wrong.

 https://run.dlang.io/is/HLhtek

 ```
 module a;

 import std.stdio;

 void main() {
     string a;
     a = a;          // Can the compiler warn at this line that 
 there is no effect?
     writeln(a);
     return;
 }
 ```
It appears a bug has already been filed (https://issues.dlang.org/show_bug.cgi?id=11970). I'll see if I can fix it. Mike
Apr 24 2018
parent Mike Franklin <slavo5150 yahoo.com> writes:
On Wednesday, 25 April 2018 at 01:20:13 UTC, Mike Franklin wrote:

 It appears a bug has already been filed 
 (https://issues.dlang.org/show_bug.cgi?id=11970).  I'll see if 
 I can fix it.
https://github.com/dlang/dmd/pull/8208 We'll see what happens.
Apr 24 2018
prev sibling parent reply Mike Franklin <slavo5150 yahoo.com> writes:
On Wednesday, 25 April 2018 at 01:08:46 UTC, Arun Chandrasekaran 
wrote:
 So I was telling my colleague that D would warn on self 
 assignment, but found that I was wrong.

 https://run.dlang.io/is/HLhtek

 ```
 module a;

 import std.stdio;

 void main() {
     string a;
     a = a;          // Can the compiler warn at this line that 
 there is no effect?
     writeln(a);
     return;
 }
 ```
Are people using self assignment of structs as a way of force-running the postblit? Is there a valid use case for that? Mike
Apr 24 2018
next sibling parent reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Wednesday, 25 April 2018 at 02:23:04 UTC, Mike Franklin wrote:
 Are people using self assignment of structs as a way of 
 force-running the postblit?  Is there a valid use case for that?

 Mike
If they are, there should be a better way of force-running the postblit.
Apr 24 2018
next sibling parent reply Meta <jared771 gmail.com> writes:
On Wednesday, 25 April 2018 at 02:32:32 UTC, Per Nordlöw wrote:
 On Wednesday, 25 April 2018 at 02:23:04 UTC, Mike Franklin 
 wrote:
 Are people using self assignment of structs as a way of 
 force-running the postblit?  Is there a valid use case for 
 that?

 Mike
If they are, there should be a better way of force-running the postblit.
You can call __postblit or __xpostblit (I think the latter is a postblit introduced via a mixin or template mixin).
Apr 24 2018
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, April 25, 2018 03:32:09 Meta via Digitalmars-d-learn wrote:
 On Wednesday, 25 April 2018 at 02:32:32 UTC, Per Nordlöw wrote:
 On Wednesday, 25 April 2018 at 02:23:04 UTC, Mike Franklin

 wrote:
 Are people using self assignment of structs as a way of
 force-running the postblit?  Is there a valid use case for
 that?

 Mike
If they are, there should be a better way of force-running the postblit.
You can call __postblit or __xpostblit (I think the latter is a postblit introduced via a mixin or template mixin).
I seriously question that it's ever a good idea to call either, but __postblit is the actual postblit constructor declared in the struct, and __xpostblit is the function that deals with calling the postblit constructor on all of the member variables and then calling __postblit. - Jonathan M Davis
Apr 25 2018
prev sibling parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Wednesday, 25 April 2018 at 03:32:09 UTC, Meta wrote:
 On Wednesday, 25 April 2018 at 02:32:32 UTC, Per Nordlöw wrote:
 On Wednesday, 25 April 2018 at 02:23:04 UTC, Mike Franklin 
 wrote:
 Are people using self assignment of structs as a way of 
 force-running the postblit?  Is there a valid use case for 
 that?

 Mike
If they are, there should be a better way of force-running the postblit.
You can call __postblit or __xpostblit (I think the latter is a postblit introduced via a mixin or template mixin).
Thanks
Apr 25 2018
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, April 25, 2018 02:32:32 Per Nordlöw via Digitalmars-d-learn 
wrote:
 On Wednesday, 25 April 2018 at 02:23:04 UTC, Mike Franklin wrote:
 Are people using self assignment of structs as a way of
 force-running the postblit?  Is there a valid use case for that?

 Mike
If they are, there should be a better way of force-running the postblit.
Under what circumstances would it be reasonable to force the postblit to run? Certainly, under any kind of normal circumstances, the postblit constructor should only be run as a result of the object being copied - which the compiler controls. - Jonathan M Davis
Apr 25 2018
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, April 25, 2018 02:23:04 Mike Franklin via Digitalmars-d-learn 
wrote:
 On Wednesday, 25 April 2018 at 01:08:46 UTC, Arun Chandrasekaran

 wrote:
 So I was telling my colleague that D would warn on self
 assignment, but found that I was wrong.

 https://run.dlang.io/is/HLhtek

 ```
 module a;

 import std.stdio;

 void main() {

     string a;
     a = a;          // Can the compiler warn at this line that

 there is no effect?

     writeln(a);
     return;

 }
 ```
Are people using self assignment of structs as a way of force-running the postblit? Is there a valid use case for that?
I would be _very_ surprised if there were a valid reason to do that. That being said, it's not necessarily true that a = a; has no effect. In the case of string that's basically true, but if the type has a postblit constructor, then it definitely can have an effect (e.g. it allocates memory or prints out that the object was copied). I don't think that it's at all likely to have a desirable effect, and I would expect for it to be fine for a = a; to be deprecated and then turned into an error on the grounds that there isn't a reasonable reason to do it and is probably a bug - especially in cases such as this(A a, B b) { a = a; b = b; } but it's likely untrue in the general case to say that that statement has no effect. - Jonathan M Davis
Apr 25 2018