Thread overview
opXxxAssign "return value"
Dec 15, 2006
Lionello Lunesu
Dec 15, 2006
Lionello Lunesu
Dec 15, 2006
Stewart Gordon
Dec 18, 2006
Lionello Lunesu
December 15, 2006
I've been following the discussion in D.announce but have lost track of the sub-thread :S

Anyway, in the end, I'd have to agree with Andrei. I think opAssign (and opMulAssign, etc) should return "void". The compiler can still implement a=b as (a.opAssign(b),a), like Andrei suggested.

The reason this would work, is that 'this' (and '*this') are not actually return values of the op-function. The caller already has the value and simply makes it available to other calls.

This way, the compiler can select the most efficient implementation: this, *this for classes, structs resp. and the implementer no longer has to write "return this" (or worse, something else!). No hidden copy that is (or not) optimized away. In fact, like I said, no return value, since the caller already has it.

L.
December 15, 2006
Forget about the '*this'.. I was mixing C++ and D again :S

L.
December 15, 2006
Lionello Lunesu wrote:
> Forget about the '*this'.. I was mixing C++ and D again :S

For structs, it still needs to be *this.

Stewart.
December 18, 2006
Stewart Gordon wrote:
> Lionello Lunesu wrote:
>> Forget about the '*this'.. I was mixing C++ and D again :S
> 
> For structs, it still needs to be *this.
> 
> Stewart.

I was referring to Walter's post, where he said that a opAssign for class C should return C (return this) and for a struct S it should return S* (also, return this). In C++ it would be S& (return *this).

L.