Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-01

record literal in FPC?

18 messages · 10 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik
to xrfang
xrfang wrote 16 lines
Hi, Is there "record literal in FPC? e.g. normally, you do: var cp : TPoint; cp.X := 0; cp.Y := 0; Edit1.CaretPos := cp; I would like to use literal directly, such as: Edit1.CaretPos := (X: 0, Y: 0); But the above syntax is wrong. Is there such thing exists?

no, but try the point function

Show quoted text 10 lines
Thanks, Shannon -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

xrfang
to Howard Page-Clark

Thanks, but what I want is to AVOID any kind of declarations. So I will use the Point() function suggested by ik.

? ?, 1? 9, 2013 at 12:26 ???Howard Page-Clark ???

xrfang wrote
I would like to use literal directly, such as: Edit1.CaretPos := (X: 0, Y: 0);

You can use a typed constant thus:

const Origin: TPoint = (x:0; y:0);
begin
..
Edit1.CaretPos := Origin;
..

Reply by email

Xiangrong Fang
to leledumbo

Hi leledumbo,

As far as I know pascal inline directive only affect the compiler, not how you write code. Could you please give an example how inline function helps in my question?

Thanks

2013/1/9 leledumbo

Show quoted text 16 lines
If you declare an inline function producing the record, the effect would be the same with that literal record. -- View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-record-literal-in-FPC-tp4028464p4028474.html Sent from the Free Pascal - Lazarus mailing list . -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130109/c95d5ac0/attachment.html>

Reply by email

Andreas Schneider
to xrfang: Hi,? Is there "record literal in FPC? e.g. normally, you do: var ? ?cp : TPoint; cp.X :=…

Hi,

you could also use the with-statement, that is pretty handy in such cases:

|with Edit1.CaretPos do
begin
  X := 0;
  Y := 0;
end;|

Best Regards,
Andreas

xrfang wrote 26 lines
Hi, Is there "record literal in FPC? e.g. normally, you do: var cp : TPoint; cp.X := 0; cp.Y := 0; Edit1.CaretPos := cp; I would like to use literal directly, such as: Edit1.CaretPos := (X: 0, Y: 0); But the above syntax is wrong. Is there such thing exists? Thanks, Shannon -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130109/e7b4dec4/attachment.html>

Reply by email

leledumbo
to Xiangrong Fang: As far as I know pascal inline directive only affect the compiler, not how you write…
As far as I know pascal inline directive only affect the compiler, not how

you write code. Could you please give an example how inline function helps in my question?

Yes it does, what I mean is to create something that Point() function does and using inline directive it's nearly the same as assigning record via literal. Well, if you know how it's done by other compilers, record literal assignment is actually broken down into field by field assignment. So if you make a function that returns the record with given arguments as record fields, with inline directive the final code would be the same. Well... maybe you'll need some other directives as well, but it's just my thought. I never really compare the generated code...

View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-record-literal-in-FPC-tp4028464p4028478.html Sent from the Free Pascal - Lazarus mailing list .

Reply by email

xrfang
to leledumbo

Well, my purpose is to make the source code look comfortable, not how the binary code is generated :)

Well, again, I got a good idea from Andreas.

Thanks to all of you.

? ?, 1? 9, 2013 at 7:45 ???leledumbo ???

As far as I know pascal inline directive only affect the compiler, not how

you write code. Could you please give an example how inline function helps in my question?

Yes it does, what I mean is to create something that Point() function does and using inline directive it's nearly the same as assigning record via literal. Well, if you know how it's done by other compilers, record literal assignment is actually broken down into field by field assignment. So if you make a function that returns the record with given arguments as record fields, with inline directive the final code would be the same. Well... maybe you'll need some other directives as well, but it's just my thought. I never really compare the generated code...

View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-record-literal-in-FPC-tp4028464p4028478.html Sent from the Free Pascal - Lazarus mailing list

Reply by email

xrfang
to Andreas Schneider: you could also use the with-statement, that is pretty handy in such cases: |with…

Hi Adreas,

I just tried, your method did not work as expected. ?It compiled ok, but didn't put the cursor at the end of the line as I wanted. ?However, assign CaretPos a new TPoint worked.

Regards,

? ?, 1? 9, 2013 at 4:47 ???Andreas Schneider ???
Hi,

you could also use the with-statement, that is pretty handy in such cases:

with Edit1.CaretPos do
begin
  X := 0;
  Y := 0;
end;
Best Regards,
Andreas

On 08.01.2013 16:30, xrfang wrote:
Hi,?

Is there "record literal in FPC? e.g. normally, you do:

var
? ?cp : TPoint;
cp.X := 0;
cp.Y := 0;
Edit1.CaretPos := cp;

I would like to use literal directly, such as:

Edit1.CaretPos := (X: 0, Y: 0);?

But the above syntax is wrong. Is there such thing exists?

Thanks,
Shannon

Reply by email

Mattias Gaertner
to xrfang
Show quoted text 16 lines
Hi Adreas, I just tried, your method did not work as expected. ?It compiled ok, but didn't put the cursor at the end of the line as I wanted. ?However, assign CaretPos a new TPoint worked. Regards, ? ?, 1? 9, 2013 at 4:47 ???Andreas Schneider ??? Hi, you could also use the with-statement, that is pretty handy in such cases: with Edit1.CaretPos do begin X := 0; Y := 0; end;

"With" does not work with record properties.

Mattias

Reply by email

Andreas Schneider
to xrfang: I just tried, your method did not work as expected. ?It compiled ok, but didn't put the…

Interesting. IIRC that worked correctly in Delphi 2010 (it's been a while ...). This /might /be a bug in FPC.

The root of the problem is how properties are handled. Afaics however, the use of /with/ should cause it to assign the final record afterwards. Apparently it doesn't - at least not always. Could be by design, could be a bug. I'll check the bug tracker later and may report that. (You can do that yourself, if you want, but sooner or later I would like that functionality to work too, so I've a personal interest in this and therefore would not mind to report it :-))

Greetings,
Andreas

xrfang wrote 61 lines
Hi Adreas, I just tried, your method did not work as expected. It compiled ok, but didn't put the cursor at the end of the line as I wanted. However, assign CaretPos a new TPoint worked. Regards, ? ?, 1? 9, 2013 at 4:47 ??,Andreas Schneider ??: > > Hi, > > you could also use the with-statement, that is pretty handy in such > cases: > > |with Edit1.CaretPos do > begin > X := 0; > Y := 0; > end;| > > Best Regards, > Andreas > > > On 08.01.2013 16:30, xrfang wrote: >> Hi, >> >> Is there "record literal in FPC? e.g. normally, you do: >> >> var >> cp : TPoint; >> >> cp.X := 0; >> cp.Y := 0; >> Edit1.CaretPos := cp; >> >> I would like to use literal directly, such as: >> >> Edit1.CaretPos := (X: 0, Y: 0); >> >> But the above syntax is wrong. Is there such thing exists? >> >> Thanks, >> Shannon >> >> >> -- >> _______________________________________________ >> Lazarus mailing list >> >> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130109/6a5f0061/attachment-0001.html>

Reply by email

xrfang
to Andreas Schneider

I don't know if it's related to the fact that TEdit.CaretPos.X (or Y) is read only, or as Mattias said, the with syntax cannot work with record anyway... :-)

This should be a problem (if any) with lazarus not fpc.

Shannon

? ?, 1? 9, 2013 at 8:27 ???Andreas Schneider ???
Interesting. IIRC that worked correctly in Delphi 2010 (it's been a while ...). This might be a bug in FPC.

The root of the problem is how properties are handled. Afaics however, the use of with should cause it to assign the final record afterwards. Apparently it doesn't - at least not always. Could be by design, could be a bug. I'll check the bug tracker later and may report that. (You can do that yourself, if you want, but sooner or later I would like that functionality to work too, so I've a personal interest in this and therefore would not mind to report it :-))

Greetings,
Andreas

On 09.01.2013 13:19, xrfang wrote:
Hi Adreas,

I just tried, your method did not work as expected. ?It compiled ok, but didn't put the cursor at the end of the line as I wanted. ?However, assign CaretPos a new TPoint worked.

Regards,

? ?, 1? 9, 2013 at 4:47 ???Andreas Schneider ???
Hi,

you could also use the with-statement, that is pretty handy in such cases:

with Edit1.CaretPos do
begin
  X := 0;
  Y := 0;
end;
Best Regards,
Andreas

On 08.01.2013 16:30, xrfang wrote:
Hi,?

Is there "record literal in FPC? e.g. normally, you do:

var
? ?cp : TPoint;
cp.X := 0;
cp.Y := 0;
Edit1.CaretPos := cp;

I would like to use literal directly, such as:

Edit1.CaretPos := (X: 0, Y: 0);?

But the above syntax is wrong. Is there such thing exists?

Thanks,
Shannon

Reply by email

Mattias Gaertner
to Andreas Schneider: Interesting. IIRC that worked correctly in Delphi 2010 (it's been a while ...). This…
Show quoted text 5 lines
Interesting. IIRC that worked correctly in Delphi 2010 (it's been a while ...). This /might /be a bug in FPC. The root of the problem is how properties are handled. Afaics however, the use of /with/ should cause it to assign the final record afterwards.

That would be nice. At the moment fpc does not do that.

Show quoted text 5 lines
Apparently it doesn't - at least not always. Could be by design, could be a bug. I'll check the bug tracker later and may report that. (You can do that yourself, if you want, but sooner or later I would like that functionality to work too, so I've a personal interest in this and therefore would not mind to report it :-))

Feel free to submit a feature request to the fpc bug tracker.

Mattias

Reply by email

Paul Ishenin
to Mattias Gaertner

09.01.13, 20:34, Mattias Gaertner ?????:

Show quoted text 7 lines
> Apparently it doesn't - at least not always. Could be by design, could > be a bug. I'll check the bug tracker later and may report that. (You can > do that yourself, if you want, but sooner or later I would like that > functionality to work too, so I've a personal interest in this and > therefore would not mind to report it :-)) Feel free to submit a feature request to the fpc bug tracker.

First check whether it works delphi mode.

Best regards,
Paul Ishenin

Reply by email

Andreas Schneider
to Paul Ishenin
First check whether it works delphi mode.

Apparently, it even works in mode objfpc. But only when the property directly reads/writes a member. If there is a setter method, it will still compile but NOT actually change the record. Neither in mode objfpc nor in mode delphi.

Example attached. I will also open a bug report.

Best Regards, Andreas -------------- next part -------------- A non-text attachment was scrubbed... Name: fpcwith.lpr Type: application/octet-stream Size: 1350 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130109/05039d9c/attachment.obj>

Reply by email

Michael Schnell
to Hans-Peter Diettrich
Hans-Peter Diettrich wrote
A "with" should evaluate to an address

IMHO this is an "implementation detail", that any compiler can handle as it likes to. It also could just work as if in the source the appropriate record elements code are prefixed by the "with" argument. That does not prevent that during compilation as well a code using "with" as a fully expanded code can be optimized to use a pointer to the record.

(I in fact never use "with" as with Delphi 7 - which I last tested on that behalf - the debugger does not easily show record elements that are defined in a "with" clause. Moreover more often than not the code with (nested) with can get rather ambiguous and confusing.)

-Michael

Reply by email