Sign in Join

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

Mailing lists / fpc-pascal / 2010-11

Changing var to out in rtl code

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Max Vlasov

Hi,
I'm one of those developers who takes hints and warnings seriously and I must say at least once a week it helps me find a bugs even before running the program. So when I got a warning about some uninitialized variable passed by reference (Delphi 5 is ok with such variable being uninitialized), I thought what I should do to fix this, and the best solution for this is changing the declaration from "var " to to "out " (CMIIW).

But is it possible to do the same (partially, gradually or in any other way) for the "legacy" code that keeps the interface compatibility with Delphi. For example TStrignList.Find(const S: string; var Index: Integer): Boolean; declares "var " so my existing fragments expecting just an result from this call, got "Hint: Local variable "Index" does not seem to be initialized" warning. But looking at the implementation details it seems it is safe to change "var Index" to "out Index" and possibly it is also safe for any delphi/lazarus code calling this method

I'm sure it is in any way not the first priority task (I can change the corresponding calling fragments), but just interesting what others think about it.

Thanks,

Max Vlasov

Reply by email

Sven Barth
to Max Vlasov
Max Vlasov wrote 21 lines
Hi, I'm one of those developers who takes hints and warnings seriously and I must say at least once a week it helps me find a bugs even before running the program. So when I got a warning about some uninitialized variable passed by reference (Delphi 5 is ok with such variable being uninitialized), I thought what I should do to fix this, and the best solution for this is changing the declaration from "var " to to "out " (CMIIW). But is it possible to do the same (partially, gradually or in any other way) for the "legacy" code that keeps the interface compatibility with Delphi. For example TStrignList.Find(const S: string; var Index: Integer): Boolean; declares "var " so my existing fragments expecting just an result from this call, got "Hint: Local variable "Index" does not seem to be initialized" warning. But looking at the implementation details it seems it is safe to change "var Index" to "out Index" and possibly it is also safe for any delphi/lazarus code calling this method I'm sure it is in any way not the first priority task (I can change the corresponding calling fragments), but just interesting what others think about it.

Here is an old bug report where "var" => "out" is discussed as well: http://bugs.freepascal.org/view.php?id=11263
I suggest you to read all commants to find the one or other pitfall regarding this (especially Jonas' comment 19338).

That said I personally think that TStringList.Find could be changed indeed, as it is a) in an objfpc unit and b) the param isn't a reference counted one. (But as I'm not a member of the FPC team, I have nothing final/definite to say here ^^)

Regards,
Sven

Reply by email

Max Vlasov
to Sven Barth
Sven Barth wrote 25 lines
On 21.11.2010 16:22, Max Vlasov wrote: > But is it possible to do the same (partially, gradually or in any other > way) for the "legacy" code that keeps the interface compatibility with > Delphi. For example TStrignList.Find(const S: string; var Index: > Integer): Boolean; declares "var " so my existing fragments expecting > just an result from this call, got "Hint: Local variable "Index" does > not seem to be initialized" warning. But looking at the implementation > details it seems it is safe to change "var Index" to "out Index" and > possibly it is also safe for any delphi/lazarus code calling this method > > Here is an old bug report where "var" => "out" is discussed as well: http://bugs.freepascal.org/view.php?id=11263 I suggest you to read all commants to find the one or other pitfall regarding this (especially Jonas' comment 19338). That said I personally think that TStringList.Find could be changed indeed, as it is a) in an objfpc unit and b) the param isn't a reference counted one. (But as I'm not a member of the FPC team, I have nothing final/definite to say here ^^) Regards, Sven

Sven, thank for sharing the link. Comment 19338 is indeed interesting, never thought about such semantic difference. Apart from TStringList.Find. calls I noticed also TStream.ReadBuffer calls that appears much frequently with constructs like TStream.ReadBuffer(ACount, SizeOf(ACount)) also giving the hints. Hope I'll be strong enough to apply all the initialization changes to the sources :)

Max Vlasov

Reply by email

Michael Van Canneyt
to Max Vlasov: I'm one of those developers who takes hints and warnings seriously and I must say at…
Max Vlasov wrote 9 lines
Hi, I'm one of those developers who takes hints and warnings seriously and I must say at least once a week it helps me find a bugs even before running the program. So when I got a warning about some uninitialized variable passed by reference (Delphi 5 is ok with such variable being uninitialized), I thought what I should do to fix this, and the best solution for this is changing the declaration from "var " to to "out " (CMIIW). But is it possible to do the same (partially, gradually or in any other way) for the "legacy" code that keeps the interface compatibility with Delphi. For example TStrignList.Find(const S: string; var Index: Integer): Boolean; declares "var " so my existing fragments expecting just an result from this call, got "Hint: Local variable "Index" does not seem to be initialized" warning. But looking at the implementation details it seems it is safe to change "var Index" to "out Index" and possibly it is also safe for any delphi/lazarus code calling this method

For this particular instance, I changed the code. (rev 16395) But in general, one must be careful with changing such things.

Michael.

Reply by email

Max Vlasov
to Michael Van Canneyt

On Sun, Nov 21, 2010 at 9:22 PM, Michael Van Canneyt <

Show quoted text 33 lines
wrote: On Sun, 21 Nov 2010, Max Vlasov wrote: Hi, > I'm one of those developers who takes hints and warnings seriously and I > must say at least once a week it helps me find a bugs even before running > the > program. So when I got a warning about some uninitialized variable passed > by reference (Delphi 5 is ok with such variable being uninitialized), I > thought > what I should do to fix this, and the best solution for this is changing > the declaration from "var " to to "out " (CMIIW). > > But is it possible to do the same (partially, gradually or in any other > way) for the "legacy" code that keeps the interface compatibility with > Delphi. > For example TStrignList.Find(const S: string; var Index: Integer): > Boolean; declares "var " so my existing fragments expecting just an result > from this > call, got "Hint: Local variable "Index" does not seem to be initialized" > warning. But looking at the implementation details it seems it is safe to > change > "var Index" to "out Index" and possibly it is also safe for any > delphi/lazarus code calling this method > For this particular instance, I changed the code. (rev 16395) But in general, one must be careful with changing such things.

Michael, great to hear, the only thing I didn't notice initially (sorry), is that it's a virtual method. The change would probably break inheritance in some cases for some developers, but I just can't imagine someone overriding TStringList.Find since its logic is completely full and content for ages and even if so, probably making such small fix should not be a problem. Sorry again.

Max Vlasov

Reply by email