Behavior of "Initialize Local Variables" setting

This forum is meant for questions and discussions about the X# language and tools
Post Reply
thilef
Posts: 14
Joined: Wed May 26, 2021 5:13 pm

Behavior of "Initialize Local Variables" setting

Post 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
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Behavior of "Initialize Local Variables" setting

Post 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...
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Re: Behavior of "Initialize Local Variables" setting

Post by robert »

Guys,
This seems to be a bug in the compiler.
Can you create a ticket for this?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Re: Behavior of "Initialize Local Variables" setting

Post by Chris »

Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply