Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Delphi is there a point to declare a parameter of type object as const?

I know the benefits of const parameters for simple types and strings in Delphi. But what about a const TStrings? Since a const TStrings parameter can have its items (content) changed, what's the point of declaring a const TStrings parameter for example? This applies to any object too.

like image 533
AlexV Avatar asked Oct 29 '19 19:10

AlexV


People also ask

How do you declare a constant in Delphi?

To declare a record constant, specify the value of each field - as fieldName: value , with the field assignments separated by semicolons - in parentheses at the end of the declaration. The values must be represented by constant expressions.

What does const mean Delphi?

Description. Delphi extends the const keyword of standard Pascal by allowing you to specify any constant-valued expression as the value of a constant and by allowing you to give a specific type for a constant.


1 Answers

The const applies only to the pointer to the TStrings object, not to the TStrings object itself. So it really makes little difference on non-ARC based platforms, especially if the code does not try to change where the pointer is pointing to. It's only real use is to document the intention of the parameter.

But, the const can make a BIG difference on ARC-based platforms. It disables implicit reference counting on the object that is passed to the parameter (just as const does for string and interface parameters).

like image 106
Remy Lebeau Avatar answered Oct 31 '22 03:10

Remy Lebeau