Does D have too many features?

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Apr 29 11:42:23 PDT 2012


On 4/29/12, Jacob Carlborg <doob at me.com> wrote:
> In principle I agree with you. But in practice this doesn't always work.
> Take this for example:
> Prints "0" and "1" as expected. If we now change "point" to a property
> like this:
> It will now print "0" and "0". This is a silently breaking change. Sure
> you can change "point" to return by reference..

This is a great point and an issue I've ran into and talked about
before. The compiler really ought to try and convert a call like this:

foo.property++;
foo.property+=10;

into e.g.:
foo.property = foo.property.opAdd(1);
foo.property = foo.property.opAdd(10);

It would make for some really nice APIs if this feature was available.
Otherwise I have to resort to template magic and delegate calls such
as:

class Widget {
    Notify!Point pos;
    this() { pos.init(&posSet); }  // if pos changes, invoke posChange
    void posChange(ref Point newPos) { newPos.normalize(); }  // e.g.
if out of bounds
}

Unfortunately that template broke in an earlier compiler regression
that I haven't noticed until recently
(http://d.puremagic.com/issues/show_bug.cgi?id=7991)


More information about the Digitalmars-d mailing list