Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Done local vars (and greedy gosub vars)

Feb
16
0
I didn't see this anywhere, but forgive me if I simply missed it. I would like to request this:

Local Variables in Gosubs

This is almost already present, in that Gosub parameter variables are local to the sub. However, there's no way to define them except in the :Sub definition. An example (trivial, but shows the point):

...
set fullstr=blah
gosub sub1 aa bb cc

:sub1 [v1 v2 v3]
set fullstr=%v1 %v2 %v3
echo you passed %fullstr
return

The fullstr var in the sub conflicts with the fullstr var outside the sub. You can hack around this by defining the sub as:

:sub [v1 v2 v3 fullstr]

However, that is clumsy. Since the shell already knows to destroy parameter vars with the Return, then it seems it should be easy to allow this:

:sub [v1 v2 v3]
local fullstr
set fullstr=%v1 %v2 %v3
echo you passed %fullstr
return

When the return is executed, the local copy of fullstr would be destroyed, leaving any instance of fullstr outside the sub intact.

In an ideal world this would be possible, too:

local myvar=some_value

However, assignment is optional. The real meat here is local scoping of vars.

It seems the only major issue with this would be what do do about duplicate definitions, as in:

:sub [myvar]
local myvar

I'd say that's an error in the local statement.

I wouldn't mind a rule that says there can be no statements between the :sub definition and a local, except for another local. So this would be legal:

:sub ...
local ...
local ...

But not this:

:sub ...
echo ...
local ...

Nor this:

:sub ...
local ...
echo ...
local ...

Considering the general syntax and style of TC, this might be a workable alternate syntax:

GOSUB ["filename"] label [variables ] /L [variables]

The optional second list of names would be local, non-parameter variables.



BTW, as I noted in a support post, for parameter vars I would like to see the last parameter variable be a greedy one. That is, if there are three parameter vars defined, and six values passed, then the third parameter variable should gobble the last four values.

Or, allow the final parameter to be suffixed with $ in order to force it to be greedy. This would be something like the positional var %n$ notation, except that in a definition:

:sub [this that other]

would work like it currently does -- only the first three values will be picked up. However:

:sub [this that other$]

would behave like a %n$ variable and consume all values from the third one to the last one.
 
grimblefritz:
...

> Local Variables in Gosubs
> ...
> The real meat here is local scoping of vars.

The feature is already available: SETLOCAL/ENDLOCAL. They work in a
subroutine the same way as in any other part of a batch program.
--
HTH, Steve
 
No, that is not the same. Using setlocal/endlocal to bracket a sub effectively kills the ability to set any global vars. Yes, it will do if you want absolutely everything local, but it is not the same as being able to explicitly define local vars.
 
grimblefritz wrote:
| No, that is not the same. Using setlocal/endlocal to bracket a sub
| effectively kills the ability to set any global vars. Yes, it will
| do if you want absolutely everything local, but it is not the same
| as being able to explicitly define local vars.

Don't forget that ENDLOCAL can export selected variables!
--
Steve
 
Back
Top