Properties

Miles _______ at _______.____
Sun Jan 11 07:33:51 PST 2009


dsimcha wrote:
> I figure the vast majority of cases are going to be primitive types anyhow (mostly
> ints),

Yes, this is very true.

> and if someone defines operator overloads such that foo += 1 produces
> totally different observable behavior than foo = foo + 1, that's just too
> ridiculously bad a design to even take seriously.

Sure. It is bad coding style, it is ugly and the programmer who does
this should be called for a meeting with his boss. But there are still
ways to have sane behavior, even in such situations. See below.

> What do you think?  Is it worth
> ignoring a few hard cases in exchange for solving most cases simply and elegantly
> and without adding any new constructs?

Instead, I think it is more sane to use temporaries.

----------
	{
	  auto tmp = __get_foo();
	  tmp += 1;
	  __set_foo(foo);
	}
----------

It is the safest this way, principle of least surprise. If the caller
does foo += 1, it will get that; if it does foo = foo + 1, it will still
get that; if it does foo.call(), again, the behavior is still sane.

We must first attack the semantics. This have sane semantics. Then let
the compiler optimize that as far as possible. The compiler inlines the
getter and setter calls, then optimizes away the temporary, etc.



More information about the Digitalmars-d mailing list