[Gambas-bugtracker] Bug #1361: Declare variables anywhere and more lexical scope
bugtracker at gambaswiki.org
bugtracker at gambaswiki.org
Sun Jul 8 14:25:15 CEST 2018
http://gambaswiki.org/bugtracker/edit?object=BUG.1361&from=L21haW4-
Tobias BOEGE reported a new bug.
Summary
-------
Declare variables anywhere and more lexical scope
Type : Request
Priority : Medium
Gambas version : Unknown
Product : Language
Description
-----------
A feature request for Gambas 4.0, but that's not an option for the "Gambas version" label.
(1) I want the ability to Dim local variables anywhere in a function:
Dim i As Integer = 12
Print i
i = 15
Dim j As Integer = i ^ 2
Print j
This is not as easy as having the parser pretend the variable declaration occured at the
top of the function, because the initialisation of j depends on statements written before
the declaration.
(2) It would be nice if IF, FOR, FOR EACH, DO, WHILE, REPEAT, CASE and WITH provided a new
lexical scope, i.e. any variable you declare in these blocks is only available (lexically-)
inside the block.
Dim i As Integer
For i = 1 To 10
Dim j As Integer = i ^ 2
Print j
Next
Print j ' error, variable j is unknown here
For i = 1 To 10
Dim j As Integer = i - 1
Print j ' allowed, this is a new lexical scope
Next
Bonus points for a documented decision on what should happen when a variable is re-declared
in a tighter lexical scope:
For i = 1 To 10
Dim i As Integer = 100
Print i
Next
To me, it would make sense to either forbid that altogether, or allow the redeclaration with
the following semantics: the outer loop uses the i from the enclosing scope, i.e. the loop
is really executed 10 times, and inside the loop you print the contents of a new variable.
(That means you can lose access to your loop variable if you want to.)
More information about the Bugtracker
mailing list