bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 13 forks source link

Globals in For Loops ( or other "blocks" ) #629

Open GWRon opened 1 year ago

GWRon commented 1 year ago

a user had an issue reported in discord. I extracted the original problem:

SuperStrict

Type TPlayer
    Field x:Int
End Type

Local list:TList = New TList()

For local o:object = EachIn list
    Global example1:TPlayer = New TPlayer
Next
print example1.x

I thought a global can be declared everywhere - but it seems it can't: Output:

Compile Error: Identifier 'example1' not found.

Same also for While-loops and surely other "blocks". And also inside "if endif".

GWRon commented 1 year ago

I am not sure if BCC should error out if someone "defines" a global somewhere where others cannot reach it. Or maybe "warn" ?

GWRon commented 1 year ago

Midimaster ammended that a global in a "block" exists for the whole lifetime of the block (eg loop)

For Local i:Int=0 To 9
    Global wert:Int=10
    wert=wert+1
    Print wert
Next

Maybe the documentation for "global" should reflect it ? Something that tells that globals "stay forever" but are limited in their scope (you can only read them in that scope)

davecamp commented 1 year ago

Maybe a better way to put it would be...

"A global within a scoped block, and its assigned/updated value, will exist for the lifetime of the application, but it is only available to be used within the defined scope and any inner scopes from where the global is defined."

Something like that 😬

On Thu, 13 Jul 2023, 19:29 Ronny Otto, @.***> wrote:

Midimaster ammended that a global in a "block" exists for the whole lifetime of the block (eg loop)

For Local i:Int=0 To 9 Global wert:Int=10 wert=wert+1 Print wert Next

Maybe the documentation for "global" should reflect it ?

— Reply to this email directly, view it on GitHub https://github.com/bmx-ng/bcc/issues/629#issuecomment-1634079469, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADYSTENOD2WNOOLG24BBX63XP7L2BANCNFSM6AAAAAA2IYOPFQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>