[Issue 9801] New: Syntax sugar for self-assignment of this arguments
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Mar 23 12:32:13 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9801
Summary: Syntax sugar for self-assignment of this arguments
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2013-03-23 12:32:11 PDT ---
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) {}
- - - - - - - - - -
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list