Does 'Local' have another purpose?

BlitzMax Forums/BlitzMax Programming/Does 'Local' have another purpose?

Russell(Posted 2005) [#1]
I was wondering why the following code snippet gives an error if the Local keyword is omitted:
Local StringArray:String[] = ["Here's","a","string"]

Print StringArray[2].length
Print SizeOf(StringArray) 

I thought Local was for declaring a variable inside a function or method to be non-global and is released when the function returns. What does that have to do with the example above?

Is Local required for some other reason(s), too?

Thanks,
Russell


Robert(Posted 2005) [#2]
Local is for declaring a variable that is only visible within the current scope, and which 'dies' when the scope is left. Every part of the program is in some scope or another, the highest scope being the main body of your code.

In Strict mode, variables must be declared as either Local or Global.

The 'scope' does not have to mean a function. It applies to For..Next, Repeat..Until, While...End While blocks as well (amongst others)


NeuralizR(Posted 2005) [#3]
Is there somewhere that scope changes are identified in the docs?

Repeat blocks have their own scope while If blocks do not, for example.


Russell(Posted 2005) [#4]
Also, the above is not in Strict mode nor in any sort of code block. So why won't it work without 'Local'?

It's no big deal, I'll just use Local all the time if I have to (unless it really is Global). Just wondered why it is required in this case.

Russell

p.s. Static would be nice as well :)


skidracer(Posted 2005) [#5]
I'd call it a bug.


Robert(Posted 2005) [#6]
Also, the above is not in Strict mode nor in any sort of code block


Yes it is in a code block. The main body of your code, when compiled, resides in a _bb_main() function.


teamonkey(Posted 2005) [#7]
Repeat blocks have their own scope while If blocks do not, for example.

Yes they do.
Strict

If(True)
	Local y:Int = 4
End If

y = 12

That won't compile the y=12 line because y is local to the If block.

Anything which finishes with an End... statement is a block with its own scope. Repeat...Until/Forever, For...Next and While...Wend are blocks too.


Russell(Posted 2005) [#8]
If you ask me, anything in the main body should be considered Global (and in most languages it is). I don't see a logical reason why Local should be required in this case.

No big deal, but it's easier to get a 'handle' on things if you know the reasoning behind it.

Russell


FlameDuck(Posted 2005) [#9]
Repeat blocks have their own scope while If blocks do not, for example.
If blocks have their own scope too. If you take effort to indent your blocks carefully, you'll notice that variable scope is in effect for the duation of the indentation (think of it like { and } in all other languages since Pascal or something, but you guys don't like visible indication of scope, [it is appearently evil] so we'll just leave it at that).

p.s. Static would be nice as well :)
Except it's called Global in Blitz. When is a Global not Global? When it's inside a type declaration... It's hard to grasp, I understand.

If you ask me, anything in the main body should be considered Global (and in most languages it is).
It isn't "Global" as such (nor is it nessecarilly in other languages). It is however Local to the "root" or "main" scope of the program.

No big deal, but it's easier to get a 'handle' on things if you know the reasoning behind it.
Well that's really the core of the problem, isn't it?