www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opXxxAssign "return value"

reply Lionello Lunesu <lio lunesu.remove.com> writes:
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.
Dec 15 2006
parent reply Lionello Lunesu <lio lunesu.remove.com> writes:
Forget about the '*this'.. I was mixing C++ and D again :S

L.
Dec 15 2006
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Lionello Lunesu wrote:
 Forget about the '*this'.. I was mixing C++ and D again :S
For structs, it still needs to be *this. Stewart.
Dec 15 2006
parent Lionello Lunesu <lio lunesu.remove.com> writes:
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.
Dec 17 2006