xsharp.eu • Behavior of "Initialize Local Variables" setting
Page 1 of 1

Behavior of "Initialize Local Variables" setting

Posted: Tue Dec 26, 2023 3:41 pm
by thilef
Hi to all,

I'm trying to remove warnings from my VO imported code.
To remove the "XS0165 Use of unassigned local variable" warning, I checked the "Initialize Local Variables" setting in the project properties.
But I get strange result comparing to the VO behavior. The "local variable initialization" seems to be done in the constructor of the parent class, and all the variables already initialized in a child class before super() or in the PreInit() method are reinitialized durng the super().

For example :

CLASS MyClass
PROTECT oBmp1 AS Bitmap
PROTECT oBmp2 AS Bitmap
END CLASS

CLASS MySubClass INHERIT MyClass

CTOR()
SELF:oBmp1 := Bitmap{ ResourceID{ "MyLogo1", _GetInst() } }
SUPER() // Step by step debugger stops on every line of the MyClass declaration
? SELF:oBmp1, oBmp2 // = null when "Initialize Local Variables is checked" in the project properties
RETURN

METHOD PreInit()
SELF:oBmp2 := Bitmap{ ResourceID{ "MyLogo2", _GetInst() } }
RETURN SELF

END CLASS

Have I misunderstood how this setting works?
Thanks

Thierry

Re: Behavior of "Initialize Local Variables" setting

Posted: Tue Dec 26, 2023 4:55 pm
by Chris
Hi Thierry,

Thanks for the report, this looks like a bug to me, the compiler should initialize only local vars (as the name suggests) and not class fields. Or that was intended Robert?

Btw, in c#, if you try to use an unassigned local, you get an error about this, instead of a warning, supposedly because the local might have an unknown value. But if you try to use a private field that never gets assigned, you get a warning instead saying:

"warning CS0649: Field 'nnn' is never assigned to, and will always have its default value 0"

This doesn't make sense to me, local and field vars should either both be guaranteed to get initialized a default value, or both shouldn't. This is why I am not a fan of this XS0165 warning and in XIDE there's an option to automatically always disable it in all apps...

Re: Behavior of "Initialize Local Variables" setting

Posted: Tue Dec 26, 2023 9:59 pm
by robert
Guys,
This seems to be a bug in the compiler.
Can you create a ticket for this?

Robert

Re: Behavior of "Initialize Local Variables" setting

Posted: Wed Dec 27, 2023 9:01 am
by Chris