Problem overloading operator for a struct with an immutable member

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 26 02:17:21 PDT 2015


On Thu, 26 Mar 2015 09:02:53 +0000, Nicolas Sicard wrote:

> On Thursday, 26 March 2015 at 04:57:55 UTC, ketmar wrote:
>>
>> by the way. do you know that you still CAN overload postincrement
>> operation? yes, the code is still here, and it works...
>> somethimes. ;-)
> 
> Thnaks. Indeed, this works:
> ---
> struct S {
>      int i;
>      immutable(Object) o;
> 
>      void opAddAssign(int j) { i += j; }
>      S opPostInc() { ++i; return this; }
>      void opAssign(S other) {}
> }
> 
> unittest {
>      S s;
>      ++s;
>      assert(s.i == 1);
>      s++;
>      assert(s.i == 2);
> }
> ---
> 
> Old operator overloading to the rescue !

just make sure you didn't add `opUnary` there, as it will kill 
`opPostInc`.

it worth nothing that `opPostInc` is not deprecated yet. ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150326/118dd76f/attachment.sig>


More information about the Digitalmars-d-learn mailing list