On 03/21/2011 12:55 AM, bearophile wrote:
Among the things I've listed about Archetype there's one interesting thing.
Class instances aren't PODs, but sometimes I prefer reference semantics and to
populate fields in a plain way, expecially for simple classes.
Time ago I and other people have suggested a syntax like (this also to avoid a
class of bugs http://d.puremagic.com/issues/show_bug.cgi?id=3878 ):
class Foo {
string x;
int y = 1;
this(this.x, this.y) {}
}
void main() {
Foo f3 = new Foo("hello", 10);
}
A simpler solution are classes with automatic constructors:
class Foo {
string x;
int y = 1;
}
void main() {
Foo f1 = new Foo(); // Good
Foo f2 = new Foo("hello"); // Good
Foo f3 = new Foo("hello", 10); // Good
}
What kind of problems are caused by this? :-)
Currently that syntax is supported for structs created as values, but not for
structs created by pointer:
struct Foo {
string x;
int y = 1;
}
void main() {
Foo* f1 = new Foo(); // OK
Foo* f2 = new Foo("hello"); // Error: no constructor for Foo
Foo* f3 = new Foo("hello", 10); // Error: no constructor for Foo
}
I definitely want some feature like that in D.
Have no idea why a default seems complicated for classes while structs have it.
On the other hand, the syntax using "this.p" constructor parameter names is
more general since it allows custom constructors:
class Point {
float x,y;
float dist;
this(this.x, this.y) {
this.dist = square(x*x + y*y);
}
}
Ideally, I would like this feature without "this." parameters, but it requires
named arguments...:
auto p = new Point(x=1, y=2);
--> auto assign to members by name, then perform additional constructor tasks.
Denis
--
_________________
vita es estrany
spir.wikidot.com