Sign in Join

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

Mailing lists / fpc-pascal / 2017-10

Const params question

19 messages · 7 people ·

Reply by email Subscribe to this list Your subscription

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

Ryan Joseph

If you declare a parameter as const does that effectively behave like passing by reference? I have some performance sensitive functions that take records as arguments and I don’t want copying to the stack so I’ve been using @ but maybe const is just as good? Thanks.

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote
If you declare a parameter as const does that effectively behave like passing by reference? I have some performance sensitive functions that take records as arguments and I don’t want copying to the stack so I’ve been using @ but maybe const is just as good? Thanks.

No, you need constref then.

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
No, you need constref then.

So for the record.... In the example given, will const make a copy of the record and constref will pass in the original record (no copy of data)?

If so, then what's the difference between constref and var? Because it sounds like they are doing the same thing - given the example.

Regards,
Graeme

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 5 lines
On 2017-10-11 11:01, Michael Van Canneyt wrote: > No, you need constref then. So for the record.... In the example given, will const make a copy of the record and constref will pass in the original record (no copy of data)?

In the case of const, you don't know. The compiler will do as it sees fit.

In the case of constref, you are forcing the compiler to use a reference (so no copy of data)

If so, then what's the difference between constref and var? Because it sounds like they are doing the same thing - given the example.

Behind the scenes, at an assembler level, it is using the same mechanism.

The difference is that you can assign something to a var parameter, but you cannot assign something to a constref.

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
In the case of const, you don't know. The compiler will do as it sees fit.

Oh wow, I really didn't know that.

....snip.... The difference is that you can assign something to a var parameter, but you cannot assign something to a constref.

Thank you Michael, that was very informative information. One is never too old to learn. ;-)

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys: Oh wow, I really didn't know that. Thank you Michael, that was very informative…
Graeme Geldenhuys wrote 12 lines
On 2017-10-11 11:36, Michael Van Canneyt wrote: > In the case of const, you don't know. The compiler will do as it sees fit. Oh wow, I really didn't know that. > ....snip.... > The difference is that you can assign something to a var parameter, > but you cannot assign something to a constref. Thank you Michael, that was very informative information. One is never too old to learn. ;-)

It is actually documented:

https://www.freepascal.org/docs-html/current/ref/refsu67.html#x179-20100014.4.4

Michael.

Reply by email

Vojtěch Čihák
to Michael Van Canneyt: In the case of const, you don't know. The compiler will do as it sees fit. In the case of…

Hi,
 
TBitBtn has property Kind, setting it to anything else than bkCustom will show the glyph (native, from widgetset).  
With TSpeedButton, you are always on your own.
 
V.
______________________________________________________________

Show quoted text 5 lines
Od: Komu: Datum: 11.10.2017 12:54 Předmět: [fpc-pascal] where's the glyph?

Hi,

LCL provides the usual Delphi equivalent components like TBitBtn and TSpeedButton  but there do not seem to be any glyphs to go with them.

Do I really have to make my own or are there some resources I have not found ?

Thanks.

Reply by email

Unknown author
to Vojtěch Čihák
Vojtěch Čihák wrote 21 lines
Hi, TBitBtn has property Kind, setting it to anything else than bkCustom will show the glyph (native, from widgetset). With TSpeedButton, you are always on your own. V. ____ Hi, LCL provides the usual Delphi equivalent components like TBitBtn and TSpeedButton  but there do not seem to be any glyphs to go with them. Do I really have to make my own or are there some resources I have not found ? Thanks.

Oh, of course. It is longer than I realised since I've used Delphi !

For the other glyphs I guess I'll have to install one of my old D1 or D2 CDs and dig them out..

Thanks for the reminder.

Reply by email

Marcos Douglas B. Santos
to Michael Van Canneyt: No, you need constref then. Michael.
Michael Van Canneyt wrote 11 lines
On Wed, 11 Oct 2017, Ryan Joseph wrote: > If you declare a parameter as const does that effectively behave like > passing by reference? I have some performance sensitive functions that > take records as arguments and I don’t want copying to the stack so I’ve > been using @ but maybe const is just as good? Thanks. No, you need constref then.

Is constref compatible with Delphi?

Regards,
Marcos Douglas

Reply by email

Michael Van Canneyt
to Marcos Douglas B. Santos
Marcos Douglas B. Santos wrote 15 lines
On Wed, Oct 11, 2017 at 7:01 AM, Michael Van Canneyt wrote: > > > On Wed, 11 Oct 2017, Ryan Joseph wrote: > >> If you declare a parameter as const does that effectively behave like >> passing by reference? I have some performance sensitive functions that >> take records as arguments and I don’t want copying to the stack so I’ve >> been using @ but maybe const is just as good? Thanks. > > > No, you need constref then. Is constref compatible with Delphi?

Delphi has recently introduced some annotation for the same purpose.

As usual, they chose to do something different than what FPC did.

Michael.

Reply by email

Sven Barth
to Unknown author: LCL provides the usual Delphi equivalent components like TBitBtn and TSpeedButton but…
Hi, LCL provides the usual Delphi equivalent components like TBitBtn and

TSpeedButton but there do not seem to be any glyphs to go with them.

Do I really have to make my own or are there some resources I have not

found ?

Lazarus/LCL questions are best asked on the lazarus mailing list ( http://lists.lazarus-ide.org/listinfo/lazarus ). While quite some of the devs there read here as well, you'll get even more feedback regarding Lazarus/LCL topics there.

Regards,
Sven

Reply by email

Unknown author
to Sven Barth: TSpeedButton but there do not seem to be any glyphs to go with them. found ? Lazarus/LCL…
Sven Barth wrote 20 lines
Am 11.10.2017 13:13 schrieb < >: > > Hi, > > LCL provides the usual Delphi equivalent components like TBitBtn and TSpeedButton  but there do not seem to be any glyphs to go with them. > > Do I really have to make my own or are there some resources I have not found ? Lazarus/LCL questions are best asked on the lazarus mailing list ( http://lists.lazarus-ide.org/listinfo/lazarus ). While quite some of the devs there read here as well, you'll get even more feedback regarding Lazarus/LCL topics there. Regards, Sven

yes, sorry. I will try to restrict questions here to fpc.

Reply by email

Sven Barth
"Ryan Joseph" wrote
> On Oct 11, 2017, at 5:01 PM, Michael Van Canneyt
> > No, you need constref then. Ok, so constref/var are the same in terms of copying. The docs say const

reduces stack size? I never label my parameters with const but maybe it’s worth it some cases.

This is basically only the case for managed parameters like strings and interfaces as such don't need an implicit try...finally-block in case of const and constref to correctly reset their reference count in case of an exception

Regards,
Sven

Reply by email