Bug 9801 – Syntax sugar for self-assignment of this arguments

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-03-23T12:32:11Z
Last change time
2024-12-13T18:05:12Z
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: dmd#17580 →

Comments

Comment #0 by bearophile_hugs — 2013-03-23T12:32:11Z
This code is repetitive, not DRY, and it's also bug-prone, it repeats names like x and y four times, and their types two times: class FooA { public int x; private int y; this(int x_, int y_) { this.x = x_; this.y = y_; } } It's bug prone because it's common to write code like this by mistake, that the D compiler doesn't see as wrong (see Issue 3878 ): this(int z_, int w_) { z = z_; w = w; } So I suggest to enhance D allowign sugar syntax like this, that produces code equivalent to FooA: class FooB { public int x; private int y; this(this.x, this.y) {} } Other languages like Scala and TypeScript support a related but different syntax. - - - - - - - - - - To keep this enhancement simple this is not allowed (the ctor {} are missing): class Foo2 { public int x; private int y; this(this.x, this.y); // error } - - - - - - - - - - Multiple constructors with self-assigning fields are allowed: class Foo3 { public int x; private int y; this(this.x) {} this(this.x, this.y) {} } - - - - - - - - - - Default arguments are allowed: class Foo4 { int x; this(this.x = 1) {} } - - - - - - - - - - C-style variadic arguments and D-style variadic arguments are not allowed to become automatic assignments. But this is allowed: class Foo5 { private int[] data; this(this.data...) {} } - - - - - - - - - - Mixing self-assigning arguments with regular arguments is allowed: class Foo6 { int x, y; this(in this.x, in int y_) { this.y = x + y_; } } - - - - - - - - - - lazy arguments are allowed: class Foo7 { int x; this(lazy this.x) {} } - - - - - - - - - - For keep the design simple, you can't add a type to self-assigned arguments: class Foo8 { int x; this(byte this.x) {} // error } But const/immutable/in/lazy/etc are allowed: class Foo9 { int x; this(in this.x) {} } - - - - - - - - - - The self-assignment syntax is allowed for all struct/class methods, not just for constructors: struct Foo10 { int x, y; int bar(this.x) { y += x; } } It's equivalent to: struct Foo10 { int x, y; int bar(int x_) { this.x = x_; y += x; } } - - - - - - - - - - If there is a pre-condition: struct Foo11 { int x, y; int bar(this.x) in { assert(x > 10); } body { y += x; } } It's equivalent to this (so the pre-condition changes the struct state): struct Foo11 { int x, y; int bar(int x_) in { this.x = x_; assert(x > 10); } body { y += x; } } Or to just this when the preconditions are not compiled in: struct Foo11 { int x, y; int bar(int x_) { this.x = x_; y += x; } } - - - - - - - - - - To keep the design clean the public/private/package/protected tag in the method signature aren't allowed: class Foo12 { public int x; this(public this.x) {} // error } - - - - - - - - - - Free functions are not allowed to use self-assignment syntax, there is no "this": int x; void bar(this.x) {} - - - - - - - - - -
Comment #1 by doob — 2013-03-24T03:01:06Z
(In reply to comment #0) > Other languages like Scala and TypeScript support a related but different > syntax. CoffeeScript as well.
Comment #2 by robert.schadek — 2024-12-13T18:05:12Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17580 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB