Jump to content

Variable declaration and scope


Recommended Posts

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

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Developers

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 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Developers

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 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...