+= property bug?

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

+= property bug?

Ryan Joseph-2
Is this a bug? It should resolve to:

d.setter(d.getter + 10)

but it doesn’t seem to be working.

========================================

{$mode objfpc}
{$modeswitch advancedrecords}

program test;

type
  TData = record
    x: integer;
  end;

type
  TVec2Helper = record helper for TData
    function Getter: integer;
    procedure Setter(newValue: integer);
    property Get: integer read Getter write Setter;
  end;

function TVec2Helper.Getter: integer;
begin
  result := x;
end;

procedure TVec2Helper.Setter(newValue: integer);
begin
  x := newValue;
end;

var
  d: TData;
begin
  d.get += 10; // ERROR: Variable identifier expected
end.

Regards,
        Ryan Joseph

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

Re: += property bug?

Bart-48
On Sun, Aug 11, 2019 at 6:33 PM Ryan Joseph <[hidden email]> wrote:

> begin
>   d.get += 10;  // ERROR: Variable identifier expected
> end.

AFAIK you cannot do that to a property (not even if it maps to a field).
Delphi does not allow it either IIRC.

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

Re: += property bug?

Free Pascal - General mailing list
In reply to this post by Ryan Joseph-2
Am 11.08.2019 um 18:33 schrieb Ryan Joseph:

> Is this a bug? It should resolve to:
>
> d.setter(d.getter + 10)
>
> but it doesn’t seem to be working.
>
> ========================================
>
> {$mode objfpc}
> {$modeswitch advancedrecords}
>
> program test;
>
> type
>    TData = record
>      x: integer;
>    end;
>
> type
>    TVec2Helper = record helper for TData
>      function Getter: integer;
>      procedure Setter(newValue: integer);
>      property Get: integer read Getter write Setter;
>    end;
>
> function TVec2Helper.Getter: integer;
> begin
>    result := x;
> end;
>
> procedure TVec2Helper.Setter(newValue: integer);
> begin
>    x := newValue;
> end;
>
> var
>    d: TData;
> begin
>    d.get += 10; // ERROR: Variable identifier expected
> end.
This is forbidden by design.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: += property bug?

Ryan Joseph-2


> On Aug 11, 2019, at 11:41 AM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> This is forbidden by design.

Why? It makes sense it should resolve to: d.setter(d.getter + 10) but maybe there’s a problem?

Regards,
        Ryan Joseph

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

Re: += property bug?

Rainer Stratmann
On Sonntag, 11. August 2019 12:21:18 CEST Ryan Joseph wrote:
> > On Aug 11, 2019, at 11:41 AM, Sven Barth via fpc-pascal
> > <[hidden email]> wrote:
> >
> > This is forbidden by design.
>
> Why? It makes sense it should resolve to: d.setter(d.getter + 10) but maybe
> there’s a problem?

For me it seems that you can solve the issue easily with existing
technologies.
Moreover the code will be better to read and to handle in my opinion.
Otherwise your code will be likewise C code with much information in the
smallest place. That is not good to handle in my opinion.
I have some experience with C code. It is not my preferred language.

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

Re: += property bug?

Alexander Grotewohl
In reply to this post by Ryan Joseph-2
d.x += 10; // makes even more sense

On 8/11/2019 2:21 PM, Ryan Joseph wrote:

>
>> On Aug 11, 2019, at 11:41 AM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>>
>> This is forbidden by design.
> Why? It makes sense it should resolve to: d.setter(d.getter + 10) but maybe there’s a problem?
>
> Regards,
> Ryan Joseph
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: += property bug?

Ryan Joseph-2


> On Aug 11, 2019, at 3:59 PM, Alexander Grotewohl <[hidden email]> wrote:
>
> d.x += 10; // makes even more sense

Ok, you win. Here’s more or less the full snippet.

type
  TRectHelper = record helper for TVec4
    private
      procedure SetAxisX(newValue: TScalar); inline;
    public
      function MinX: TScalar; inline;
      function MinX: TScalar; inline;
      property AxisX: TScalar read MinX write SetAxisX;
  end;

function TRectHelper.MinX: TScalar;
begin
  result := v[0];
end;

function TRectHelper.MaxX: TScalar;
begin
  result := v[2];
end;

procedure TRectHelper.SetAxisX(newValue: TScalar);
begin
  v[0] += newValue;
  v[2] += newValue;
end;

==============================

rect.axisX += 10 is a compound statement. See it makes perfect sense. :) I still don’t understand why the compiler and Delphi apparently thinks this is such a crazy idea. It's just normal stuff imo.

Regards,
        Ryan Joseph

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

Re: += property bug?

Michael Van Canneyt


On Sun, 11 Aug 2019, Ryan Joseph wrote:

>
>
>> On Aug 11, 2019, at 3:59 PM, Alexander Grotewohl <[hidden email]> wrote:
>>
>> d.x += 10; // makes even more sense
>
>
> rect.axisX += 10 is a compound statement. See it makes perfect sense. :) I still don’t understand why the compiler and Delphi apparently thinks this is such a crazy idea. It's just normal stuff imo.

This question pops up from time to time since 15 years if not more.

Some operators need actual variable addresses. A property does not have an address.
It also becomes difficult if you have interfaces & classes or other
overloaded things.

The compiler people can give a more detailed answer.

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

Re: += property bug?

Martok
Am 12.08.2019 um 09:40 schrieb Michael Van Canneyt:
>
> This question pops up from time to time since 15 years if not more.

Fun fact: questions asking why this doesn't work have a higher Google ranking
than the manual for "COPERATORS" ;-)

@Ryan: as with all things FPC, ignore the manual, read the parser:
<https://github.com/graemeg/freepascal/blob/4e6c609/compiler/pexpr.pas#L221>

Turns out c-operators do not actually translate to their long form but instead
have special, duplicated handling. Because of course they do.


--
Regards,
Martok


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

Re: += property bug?

Free Pascal - General mailing list
Martok <[hidden email]> schrieb am Mo., 12. Aug. 2019, 10:18:
Am 12.08.2019 um 09:40 schrieb Michael Van Canneyt:
>
> This question pops up from time to time since 15 years if not more.

Fun fact: questions asking why this doesn't work have a higher Google ranking
than the manual for "COPERATORS" ;-)

@Ryan: as with all things FPC, ignore the manual, read the parser:
<https://github.com/graemeg/freepascal/blob/4e6c609/compiler/pexpr.pas#L221>

Turns out c-operators do not actually translate to their long form but instead
have special, duplicated handling. Because of course they do.

The code you linked converts "a += b" to "tmp := @a; tmp^ := tmp^ + b", so except for using a temp to avoid duplicate calculation of "a" in how far is this not the long form? 

Regards, 
Sven 

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

Re: += property bug?

Martok
Am 12.08.2019 um 11:31 schrieb Sven Barth via fpc-pascal:
> The code you linked converts "a += b" to "tmp := @a; tmp^ := tmp^ + b", so
> except for using a temp to avoid duplicate calculation of "a" in how far is this
> not the long form?

No, I meant it doesn't re-parse as its long form, which is why it has some
limitations. In this case for example handle_propertysym does not know it is
actually seeing an assignment.

I don't think the compiler can do this? Macros are expanded on the scanner
level, token stream injection is not a thing in FPC, AFAICS?

--
Regards,
Martok


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

Re: += property bug?

Free Pascal - General mailing list
Martok <[hidden email]> schrieb am Di., 13. Aug. 2019, 14:44:
Am 12.08.2019 um 11:31 schrieb Sven Barth via fpc-pascal:
> The code you linked converts "a += b" to "tmp := @a; tmp^ := tmp^ + b", so
> except for using a temp to avoid duplicate calculation of "a" in how far is this
> not the long form?

No, I meant it doesn't re-parse as its long form, which is why it has some
limitations. In this case for example handle_propertysym does not know it is
actually seeing an assignment.

I don't think the compiler can do this? Macros are expanded on the scanner
level, token stream injection is not a thing in FPC, AFAICS?

It could be done, but property support for the C operators is not *desired*. See the entry of the function you posted: properties are explicitly rejected. 

In general the C operators are not desired. They where added way in the past in a weak moment and I don't think they'd be added today if FPC wouldn't have them already. 

Regards, 
Sven 

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

Re: += property bug?

Jonas Maebe-3
In reply to this post by Martok
On 13/08/2019 14:44, Martok wrote:
> Am 12.08.2019 um 11:31 schrieb Sven Barth via fpc-pascal:
>> The code you linked converts "a += b" to "tmp := @a; tmp^ := tmp^ + b", so
>> except for using a temp to avoid duplicate calculation of "a" in how far is this
>> not the long form?
>
> No, I meant it doesn't re-parse as its long form, which is why it has some
> limitations. In this case for example handle_propertysym does not know it is
> actually seeing an assignment.

If the compiler would do that, the behaviour could change from what you
would expect from glancing over the code, because the left-hand side
would be evaluated twice instead of once.


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

Re: += property bug?

Ryan Joseph-2
In reply to this post by Free Pascal - General mailing list


> On Aug 13, 2019, at 11:09 AM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> In general the C operators are not desired. They where added way in the past in a weak moment and I don't think they'd be added today if FPC wouldn't have them already.
>

Seriously? why is i := i + 1 better than i += 1 ? just more typing for such a simple operation. All languages I use have adopted this syntax and for good reason.

Regards,
        Ryan Joseph

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

Re: += property bug?

wkitty42
On 8/14/19 10:54 AM, Ryan Joseph wrote:
> Seriously? why is i := i + 1 better than i += 1 ? just more typing for such a
> simple operation. All languages I use have adopted this syntax and for good
> reason.

good reason?? because someone is too lazy to type 4 more characters? yes, i'm
counting the readability spaces which could easily be left out...

/me tightens belt on asbestos britches...


--
  NOTE: No off-list assistance is given without prior approval.
        *Please keep mailing list traffic on the list unless*
        *a signed and pre-paid contract is in effect with us.*
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: += property bug?

Ryan Joseph-2


> On Aug 14, 2019, at 11:41 AM, [hidden email] wrote:
>
> good reason?? because someone is too lazy to type 4 more characters? yes, i'm counting the readability spaces which could easily be left out...

yes, that’s exactly why. Programers got sick of wasting time typing redundant characters so they attacked the problem at the root. It’s so intuitive that basically all languages have adopted the syntax. I’m not saying you should but it’s going to  hurt Pascal if we ignore these kinds of trends.

Regards,
        Ryan Joseph

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

Re: += property bug?

Joost van der Sluis
Op 14-08-19 om 17:45 schreef Ryan Joseph:
>> On Aug 14, 2019, at 11:41 AM, [hidden email] wrote:
>>
>> good reason?? because someone is too lazy to type 4 more characters? yes, i'm counting the readability spaces which could easily be left out...
>
> yes, that’s exactly why. Programers got sick of wasting time typing redundant characters so they attacked the problem at the root. It’s so intuitive that basically all languages have adopted the syntax. I’m not saying you should but it’s going to  hurt Pascal if we ignore these kinds of trends.

Roflol... yeah... people do not use Pascal because they have to type:
i := i + 1;

Sure.

Thanks for the laugh, though. But completely off-topic. I shouldn't have
responded.

Regards,

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

Re: += property bug?

Ryan Joseph-2


> On Aug 14, 2019, at 11:52 AM, Joost van der Sluis <[hidden email]> wrote:
>
> Roflol... yeah... people do not use Pascal because they have to type:
> i := i + 1;
>
> Sure.

I’m once again shocked that anyone would be against such syntaxes as += so maybe the compiler needs to put them behind a modeswitch. Given what Sven said I’m surprised they didn’t do this from the start. Even “out” is behind a mode switch after all.

Regards,
        Ryan Joseph

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

Re: += property bug?

Rainer Stratmann
In reply to this post by Ryan Joseph-2
On Mittwoch, 14. August 2019 11:45:20 CEST Ryan Joseph wrote:
> > On Aug 14, 2019, at 11:41 AM, [hidden email] wrote:
> >
> > good reason?? because someone is too lazy to type 4 more characters? yes,
> > i'm counting the readability spaces which could easily be left out...
> yes, that’s exactly why. Programers got sick of wasting time typing
> redundant characters so they attacked the problem at the root.

I am so sick of typing all the characters...

No, your eyes get sick if you have to read C code.

If you had a real problem where there is no solution at the moment I could
understand you. But there is no problem, and no one is preventing you from
writing good and readable code.

> It’s so
> intuitive that basically all languages have adopted the syntax.

That is not true.

> I’m not
> saying you should but it’s going to  hurt Pascal if we ignore these kinds
> of trends.

Also not true.

>
> Regards,
> Ryan Joseph
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


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

Re: += property bug?

Ryan Joseph-2


> On Aug 14, 2019, at 12:04 PM, Rainer Stratmann <[hidden email]> wrote:
>
>> It’s so
>> intuitive that basically all languages have adopted the syntax.
>
> That is not true

All languages I use have them: Pascal, C, PHP, C#, Swift, Python, JavaScript. These are some of the most popular languages in the world right now. You’re fighting a losing battle sir.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12