Properties

dsimcha dsimcha at yahoo.com
Wed Jan 7 17:45:03 PST 2009


== Quote from BCS (ao at pathlink.com)'s article
> Reply to Vishaal,
> > Properties, such as array.length, should return lvalues to allow:
> > a.length += 8;
> > or other similar statements.
> I think there is a (long standing) bug report about that one. Maybe if enough
> people gripe about it it will get fixed! (NOT said sarcastically!)

Yeah, this has been mentioned in the past before.  The most obvious way to make it
work w/o any breaking or significantly bulk-adding changes would be to define
foo.length += 8 to mean foo = foo.length() + 8, or foo.length++ mean foo =
foo.length + 1.  This would be pure syntactic sugar, as it is when working with
primitives.

One problem that comes to mind is if, instead of length, which is presumably some
kind of integer, you have a property for some user defined type with operator
overloading.  This user-defined type could define opAdd to do something
arbitrarily different from opAddAssign.  Even if we assume that no reasonable
programmer would do this and treat this as a "who cares?" corner case, there's
still the problem of opInc and opAddAssign being much cheaper in some cases than
opAdd.  For example, in some cases opAdd might require copying of a whole bunch of
stuff, where opInc or opAddAssign just increments a single primitive under the hood.

Bottom line is, unless we assume that properties of user-defined types aren't
important, we need a better solution.  Oh yeah, and on the more special case of
arrays, which the compiler already treats as special, yes, foo.length += 2 should
be legal.  As far as I can tell, this is a no-brainer.



More information about the Digitalmars-d mailing list