[Issue 1596] op*Assign should return void

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 8 15:32:32 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=1596


Stewart Gordon <smjg at iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg at iname.com


--- Comment #9 from Stewart Gordon <smjg at iname.com> 2011-07-08 15:27:21 PDT ---
(In reply to comment #0)
> Experience with C++ shows that user-defined implementations of 
> assignment operators always return *this, and that there is 
> absolutely no reason to return anything else.

What's C++ to do with anything?  This is D.  You can't just blindly apply
principles learned from one language to another without understanding how the
principles depend on characteristics of the language.

In this instance, from what I can make out it is because C++ isn't garbage
collected, so you're bound to want to implement such operations by modifying
the object in-place rather than creating a new one.  Arithmetical classes
(which should be immutable, so that they behave as values) are an example of
something that can benefit from the latter in a garbage collected language such
as D, if only the small change I describe below is made.

> The rule belongs to the language. Require all op*Assign to return void. If a
> value is required on the caller side, pass the left-hand side of the operation.

I agree that the design of op*Assign is flawed, but disagree with this change.

Here's how it should work IMO:

- op*Assign (including plain opAssign) may return either void or typeof(this).

- If it's void, the AssignExpression calls this op*Assign and then returns its
lvalue.

- If it's typeof(this), the AssignExpression calls this op*Assign, sets the
lvalue to what op*Assign returns and then returns it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list