Overloading the increment operator (+=)

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Overloading the increment operator (+=)

etrusco
Hello,
If write a declaration without result it complains about invalid
syntax; if I add a result it says "impossible operator overload".
Does FPC allow overloading the += operator? If not, why?

Best regards,
Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloading the increment operator (+=)

Marcos Douglas B. Santos
2011/9/10 Flávio Etrusco <[hidden email]>
>
> Hello,
> If write a declaration without result it complains about invalid
> syntax; if I add a result it says "impossible operator overload".
> Does FPC allow overloading the += operator? If not, why?

You do not need overload the += operator, just do it in := operator.

Marcos Douglas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloading the increment operator (+=)

Jonas Maebe-2
In reply to this post by etrusco

On 10 Sep 2011, at 22:32, Flávio Etrusco wrote:

> Does FPC allow overloading the += operator? If not, why?

It is translated into "x:=x+y" at the parser level, and overload resolving happens later. So once you overload "+" (and := if required), "+=" should also work.


Jonas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloading the increment operator (+=)

etrusco
On Sun, Sep 11, 2011 at 9:22 AM, Jonas Maebe <[hidden email]> wrote:

>
> On 10 Sep 2011, at 22:32, Flávio Etrusco wrote:
>
>> Does FPC allow overloading the += operator? If not, why?
>
> It is translated into "x:=x+y" at the parser level, and overload resolving happens later. So once you overload "+" (and := if required), "+=" should also work.
>
>
> Jonas
>

Yes, I realize this. I'm revisiting the "StringBuilder" issue, and
"+=" can't be efficiently implemented with two operations (since
concatenating the operand would be unexpected to the user...).

-Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloading the increment operator (+=)

Jürgen Hestermann


Flávio Etrusco schrieb:
> "+=" can't be efficiently implemented with two operations (since
> concatenating the operand would be unexpected to the user...).
>
>  
Why not use "inc(x,y)"?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Overloading the increment operator (+=)

etrusco
On Mon, Sep 12, 2011 at 2:25 AM, Jürgen Hestermann
<[hidden email]> wrote:
>
>
> Flávio Etrusco schrieb:
>>
>> "+=" can't be efficiently implemented with two operations (since
>> concatenating the operand would be unexpected to the user...).
>
> Why not use "inc(x,y)"?
> _______________________________________________

AFAICS it's not possible either. (Notice I'm talking about operating
on an 'object').

What I meant is:
    MyStringBuffer += 'String';
Is replaced with:
    MyStringBuffer := MyStringBuffer + 'String';
What if is was:
    MyStringBuffer := OtherStringBuffer + 'String';

So concatenating the 'String' in the StringBuffer's internal buffer
would be unexpected IMO (but maybe just making it safe, even though
unexpected, would be ok).
Also, since the assignment operator for same type can't overload, I
can't make adjustments to avoid "sharing" the buffer so I'm left with
using an AnsiString as buffer, or use some self-pointing canary which
must be checked on every buffer operation.

-Flávio
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal