1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

local vars (and greedy gosub vars)

Discussion in 'Suggestions' started by grimblefritz, May 16, 2009.

  1. grimblefritz

    Joined:
    Feb 25, 2009
    Messages:
    16
    Likes Received:
    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.
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    grimblefritz:
    ...

    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
     
  3. grimblefritz

    Joined:
    Feb 25, 2009
    Messages:
    16
    Likes Received:
    0
    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.
     
  4. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    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
     

Share This Page