MazeM

Variable declaration and scope

7 posts in this topic

#1 ·  Posted

Hi

I have this script, and I really wonder why the output is what it is:

Opt("MustDeclareVars", 0)
Func foo()
   $bar = "foo"
   Local $bar
   ConsoleWrite("Func foo(), bar=" & $bar & @CRLF)
   $bar = "bar"
   ConsoleWrite("Func foo(), bar=" & $bar & @CRLF)
EndFunc
foo()
Local $bar = "zoo"
foo()
ConsoleWrite("bar=" & $bar & @CRLF)
;Outout:
;Func foo(), bar=foo
;Func foo(), bar=bar
;Func foo(), bar=
;Func foo(), bar=bar
;bar=foo
;>Exit code: 0

The behavior of the function foo() changes depending on a local variable declared outside of the function - why?

Fixing the error is obvious in this short example, but at longer scripts this is not easy to spot.

Maze

Share this post


Link to post
Share on other sites



#2 ·  Posted

First Call to foo():

  • $bar = "foo" statement creates a Local scope $bar variable
  • Local $bar doesn't do anything as it is already implicitly created
  • ConsoleWrite shows "foo"
  • $bar is set to "bar" 
  • ConsoleWrite shows "bar"

Then you create a Global scope $bar with this line: Local $bar = "zoo" ...  as Local in the Main script really is a Global scope.
Second Call to foo():

  • $bar = "foo" statement updated the Global $bar variable
  • Local $bar creates an empty $bar variable with a Local scope
  • ConsoleWrite shows ""
  • $bar is set to "bar" 
  • ConsoleWrite shows "bar"

Main script ConsoleWrite shows "foo" as that was set in the last Foo() call.

Hope this makes sense. :)

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted

Quote

Then you create a Global scope $bar with this line: Local $bar = "zoo" ...  as Local in the Main script really is a Global scope.

This explains what's happening, but it doesn't really make sense. Creating a global variable with the local keyword is something I didn't expect.

Share this post


Link to post
Share on other sites

#4 ·  Posted

That has been a design choice in the past, but understand this could be confusing for people programming in other languages.

When using au3check, to check the source code,  with parameter "-w 4" you will get a report about this:

test.au3"(12,7) : warning: 'Local' specifier in global scope.
Local $bar
~~~~~~^

Jos 

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted

Interesting option, but editing this would not fix the different output on these two calls. Only if the variable declarations were moved to the top of every function by the compiler (as in js) it would give a consistent output: Maybe this could be implemented once.

I usually use

Opt("MustDeclareVars", 1)

In SciTe-Lite pressing Ctrl+F5 will test the code with ' au3check -d'. Is it possible to configure the '-w'-options in the sourcecode somehow?

Maze

Share this post


Link to post
Share on other sites

#6 ·  Posted

When using the full SciTE4AutoIt3, you could use the #AutoIt3Wrapper directive to add parameters/options to any of the supported tools.. in this case:

#AutoIt3Wrapper_Au3Check_Parameters= -w 4

Jos 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted

:):D:lmao:

Thanks!

This will be my default setting for all scripts.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now