Operator overloading -- lets collect some use cases

Bill Baxter wbaxter at gmail.com
Tue Dec 30 23:02:32 PST 2008


2008/12/31 Don <nospam at nospam.com>:
> Weed wrote:
>>
>> Frits van Bommel пишет:
>>>
>>> Don wrote:
>>>>
>>>> Frits van Bommel wrote:
>>>>>
>>>>> Don wrote:
>>>>>>
>>>>>> A straightforward first step would be to state in the spec that "the
>>>>>> compiler is entitled to assume that X+=Y yields the same result as
>>>>>> X=X+Y"
>>>>>
>>>>> That doesn't hold for reference types, does it?
>>>>
>>>> I thought it does? Got any counter examples?
>>>
>>> For any class type, with += modifying the object and + returning a new
>>> one:
>>
>> The += operator too should return the object (usually "this")
>
> ALWAYS 'this'. It's another feature of operator overloading which is
> redundant.

In D1 I always return void for += in structs because you can't return
"this" by reference, and the compiler doesn't seem to consider it an
error to modify the result in a way that has no side effects.  So I
just consider it too error prone to have += return anything.  Better
to get a compiler error when you try to do something cute like   y = 2
+ (x+=3).

In D2 with ref returns the situation might be different.  But I
basically never find myself needing a return value from +=.

--bb


More information about the Digitalmars-d mailing list