digitalmars.D.learn - Warning on self assignment
- Arun Chandrasekaran (14/14) Apr 24 2018 So I was telling my colleague that D would warn on self
- Mike Franklin (6/20) Apr 24 2018 It appears a bug has already been filed
- Mike Franklin (3/6) Apr 24 2018 https://github.com/dlang/dmd/pull/8208
- Mike Franklin (5/19) Apr 24 2018 Are people using self assignment of structs as a way of
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (3/6) Apr 24 2018 If they are, there should be a better way of force-running the
- Meta (3/12) Apr 24 2018 You can call __postblit or __xpostblit (I think the latter is a
- Jonathan M Davis (6/20) Apr 25 2018 I seriously question that it's ever a good idea to call either, but
- =?UTF-8?B?Tm9yZGzDtnc=?= (2/15) Apr 25 2018 Thanks
- Jonathan M Davis (7/14) Apr 25 2018 Under what circumstances would it be reasonable to force the postblit to
- Jonathan M Davis (22/48) Apr 25 2018 I would be _very_ surprised if there were a valid reason to do that. Tha...
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
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
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
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
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? MikeIf they are, there should be a better way of force-running the postblit.
Apr 24 2018
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:You can call __postblit or __xpostblit (I think the latter is a postblit introduced via a mixin or template mixin).Are people using self assignment of structs as a way of force-running the postblit? Is there a valid use case for that? MikeIf they are, there should be a better way of force-running the postblit.
Apr 24 2018
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: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 DavisOn Wednesday, 25 April 2018 at 02:23:04 UTC, Mike Franklin wrote:You can call __postblit or __xpostblit (I think the latter is a postblit introduced via a mixin or template mixin).Are people using self assignment of structs as a way of force-running the postblit? Is there a valid use case for that? MikeIf they are, there should be a better way of force-running the postblit.
Apr 25 2018
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:ThanksOn Wednesday, 25 April 2018 at 02:23:04 UTC, Mike Franklin wrote:You can call __postblit or __xpostblit (I think the latter is a postblit introduced via a mixin or template mixin).Are people using self assignment of structs as a way of force-running the postblit? Is there a valid use case for that? MikeIf they are, there should be a better way of force-running the postblit.
Apr 25 2018
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: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 DavisAre people using self assignment of structs as a way of force-running the postblit? Is there a valid use case for that? MikeIf they are, there should be a better way of force-running the postblit.
Apr 25 2018
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: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 DavisSo 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?
Apr 25 2018