Small issue with pre-initializing var to nil

As written I get a warning in line 4, it expected string but got nil. I know that I can just remove the “= nil” and everything will work. Not sure if this can be fixed, feel free to tell me my code sucks if I shouldn’t be doing that. I like to pre-initialize everything AND I don’t like to see warnings. :slight_smile:

local scene = nil  
function myClass:rmScene()  
 if scene then   
 storyboard.removeScene(scene)  
 end  
end  

[import]uid: 167207 topic_id: 31883 reply_id: 331883[/import]

Not 100% sure here, but I think you need to remove the scene first before making it nil.

I think it still thinks that scene is true because it hasn’t been removed from the memory yet.

Maybe try

[lua]function myClass:rmScene()
if scene then
storyboard.removeScene(scene)
local scene = nil
end
end[/lua] [import]uid: 62706 topic_id: 31883 reply_id: 127185[/import]

What I’ve written is perfectly valid and works as expected. It’s just a test case. The issue is that Lua Glider pops a warning. [import]uid: 167207 topic_id: 31883 reply_id: 127188[/import]

Oh sorry, my mistake. It’s been a long day! [import]uid: 62706 topic_id: 31883 reply_id: 127190[/import]

@CraftyDeano
Thanks for your help!

@AndyTarkinson,
Thanks for the code sample, it helps to see a concrete example. The reason Glider is throwing a warning is because of how the parser system works right now, we admit it is a bit broken because it just parses the file top to bottom without any regard for the flow of the program. We are working on parser v 2 that will take into account program flow if possible. For example, it will try to parse the file in the order of the functions that are called. However, even this approach wont be perfect because some functions are called via event listeners that depend on user input and this is just impossible to predict with static code analysis alone. But it should greatly reduce the number of false positives such as this one. You can turn off warnings such as this via the options menu ->Glider options->editor->warnings.

Regards,
M.Y. Developers [import]uid: 55057 topic_id: 31883 reply_id: 127234[/import]

That’s cool, I was guessing it might be a tricky one to fix. I’ll just keep throwing up test cases as I find them so you are aware. I’m fairly new to Lua so I may not always be using best practices, but I try to figure them out as I go along. Glider is the best IDE for my style, I’ll surely be exercising it well. :slight_smile: [import]uid: 167207 topic_id: 31883 reply_id: 127242[/import]

@AndyTarkinson
Just a general lua hint. We like to put all of our local forward declarations at the beginning of the file. We also try to split up forward declarations from declarations that need initialization:

--file requires  
local var1, var2, var3, var4 --these are implicitly nil  
local num1,num2,num3,num4 = 0,0,0,0 --these are initialized  
--rest of the file  

you do not need to explicitly set them to nil, the lua virtual machine will automatically do it. Lua is very free-form, there is no one way to do anything. There are even countless ways to do object oriented programming (ie metatables, factories) making our job especially difficult.

Regards,
M.Y. Developers [import]uid: 55057 topic_id: 31883 reply_id: 127250[/import]

I’m a big fan of TIMTOWTDI coming from a background in Perl. I’ve already come up with my own versions of classes with metatables (obviously based on several popular examples) as well as inheritance and singletons. Funny thing is I’ve always used OOP principles even way back when programming just plain old C code, it’s always been my style. Lua fits me well in many of the same ways that Perl did. [import]uid: 167207 topic_id: 31883 reply_id: 127265[/import]

@AndyTarkinson,

Great! OOP is great but we wish it was a bit more structured like Java for instance. Just be aware of the performance implications of over using too many layers of inheritance in your code. You may be interested in annotating your code so that the IDE can be aware of your classes and provide you with context sensitive autocomplete. You can just control click on a library file (say display.newRect for instance) to see how the annotation system works.

Regards,
M.Y. Developers
[import]uid: 55057 topic_id: 31883 reply_id: 127274[/import]

Not 100% sure here, but I think you need to remove the scene first before making it nil.

I think it still thinks that scene is true because it hasn’t been removed from the memory yet.

Maybe try

[lua]function myClass:rmScene()
if scene then
storyboard.removeScene(scene)
local scene = nil
end
end[/lua] [import]uid: 62706 topic_id: 31883 reply_id: 127185[/import]

What I’ve written is perfectly valid and works as expected. It’s just a test case. The issue is that Lua Glider pops a warning. [import]uid: 167207 topic_id: 31883 reply_id: 127188[/import]

Oh sorry, my mistake. It’s been a long day! [import]uid: 62706 topic_id: 31883 reply_id: 127190[/import]

@CraftyDeano
Thanks for your help!

@AndyTarkinson,
Thanks for the code sample, it helps to see a concrete example. The reason Glider is throwing a warning is because of how the parser system works right now, we admit it is a bit broken because it just parses the file top to bottom without any regard for the flow of the program. We are working on parser v 2 that will take into account program flow if possible. For example, it will try to parse the file in the order of the functions that are called. However, even this approach wont be perfect because some functions are called via event listeners that depend on user input and this is just impossible to predict with static code analysis alone. But it should greatly reduce the number of false positives such as this one. You can turn off warnings such as this via the options menu ->Glider options->editor->warnings.

Regards,
M.Y. Developers [import]uid: 55057 topic_id: 31883 reply_id: 127234[/import]

That’s cool, I was guessing it might be a tricky one to fix. I’ll just keep throwing up test cases as I find them so you are aware. I’m fairly new to Lua so I may not always be using best practices, but I try to figure them out as I go along. Glider is the best IDE for my style, I’ll surely be exercising it well. :slight_smile: [import]uid: 167207 topic_id: 31883 reply_id: 127242[/import]

@AndyTarkinson
Just a general lua hint. We like to put all of our local forward declarations at the beginning of the file. We also try to split up forward declarations from declarations that need initialization:

--file requires  
local var1, var2, var3, var4 --these are implicitly nil  
local num1,num2,num3,num4 = 0,0,0,0 --these are initialized  
--rest of the file  

you do not need to explicitly set them to nil, the lua virtual machine will automatically do it. Lua is very free-form, there is no one way to do anything. There are even countless ways to do object oriented programming (ie metatables, factories) making our job especially difficult.

Regards,
M.Y. Developers [import]uid: 55057 topic_id: 31883 reply_id: 127250[/import]

I’m a big fan of TIMTOWTDI coming from a background in Perl. I’ve already come up with my own versions of classes with metatables (obviously based on several popular examples) as well as inheritance and singletons. Funny thing is I’ve always used OOP principles even way back when programming just plain old C code, it’s always been my style. Lua fits me well in many of the same ways that Perl did. [import]uid: 167207 topic_id: 31883 reply_id: 127265[/import]

@AndyTarkinson,

Great! OOP is great but we wish it was a bit more structured like Java for instance. Just be aware of the performance implications of over using too many layers of inheritance in your code. You may be interested in annotating your code so that the IDE can be aware of your classes and provide you with context sensitive autocomplete. You can just control click on a library file (say display.newRect for instance) to see how the annotation system works.

Regards,
M.Y. Developers
[import]uid: 55057 topic_id: 31883 reply_id: 127274[/import]