[Issue 1835] New: typeof(this) should return the type of object or some other typeof() is needed
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Feb 14 03:25:48 PST 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1835
Summary: typeof(this) should return the type of object or some
other typeof() is needed
Product: D
Version: unspecified
Platform: Other
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: onlystupidspamhere at yahoo.se
Consider the following code:
class A {
A foo() { return this; }
}
class B : A {
B bar() { return this; }
}
void main() {
auto b = new B;
b.foo().bar(); // We would like to chain methods like this
}
Currently this is not possible without CRTP (fugly) / mixins (not a good idea)
/ casts (yucky). Also typeof(this) returns A in class A.. Why can't we allow
this:
class A {
typeof(this) foo() { return this; } // here the return type would depend on
the actual runtime type of the object
}
class B : A {
B bar() { return this; }
}
void main() {
auto b = new B;
b.foo().bar(); // We would like to chain methods like this
}
Similar problems occur with common graph data structures. E.g. if we have nodes
and edges and inherit from both of them. Now going from SubNode to SubNode
won't be possible without the tricks mentioned above because of the type
system.
This is a very useful feature, see Scala language docs for more info.
--
More information about the Digitalmars-d-bugs
mailing list