[Issue 1835] 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 05:23:14 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1835





------- Comment #1 from andrei at metalanguage.com  2008-02-14 07:23 -------
This proposal has a lot of merit. A better example than chaining is the clone
function:

class Root
{
    abstract typeof(this) clone();
    ...
}

If such a function were defined, then all derived classes would be required to
implement it (which is a Good Thing) and would also be forced to return the
same static type as themselves (which again is a Good Thing).

Unfortunately, this does not port nicely to functions that accept an
equivariant parameter, such as opCmp and opEquals:

class Root
{
    int opCmp(typeof(this) another);
    int opEquals(typeof(this) another);
    ...
}

The intent is to have the comparison operations accept the same type as the
actual static type of the object. But such a signature cannot be checked
statically - a runtime check would have to be inserted.

Nonetheless, I'll bring it up with Walter.


-- 



More information about the Digitalmars-d-bugs mailing list