[fpc-devel] an idea about "const param: string"
Martin
fpc at mfriebe.de
Sun May 29 00:17:15 CEST 2011
On 28/05/2011 22:47, Alexander Klenin wrote:
> Perhaps an easier thing to do is to make const string param optimization
> controllable by compiler switch and/or directive.
> This way, users who prefer safe behavior, can get it,
> and those who are willing to take a risk still get a (weaker) tool to
> detect the error --
> by checking if the program works with the switch but crashes without it.
>
The only thing that the const param (afaik) does to a string is the
optimization to skip the ref count (and with that the implicit try
finallyt block, that the compiler generates).
Otherwise strings are already pointer, so no need to make them pointer
to pointer using "const" (not even sure if it will double pointer it,
probably not).
So users who do not want this, just leave the "const" away.
Also checking for crashes is not that simple. It doesn't always lead to
a crash. Or not immediately, so you get a 100 runs that are fine, you
publish the app, and crashes will happen.
If for some reason you wish to use "const" optimization on strings, then
it would be good to have a tool to check it's safety. Of course, as with
range-checks, and others too, it doesn't always happen, the check is
just another chance to find an issue, not a guarantee...
Yet, there was that link to "who to shoot yourself in the foot with
different programming languages" => the pascal compiler does not let you
shoot yourself in the foot.
Well it does, it gives you "const s: string" params....
Martin
More information about the fpc-devel
mailing list