On Thu, Jul 12, 2018 at 12:37 PM Rony G. Flatscher <rony.flatsc...@wu.ac.at>
wrote:
> Here just another example that should demonstrate what can be done with it.
>
> a="this value comes from the main program ('prolog')"
> say "a="pp(a)
> say
>
> call one >a -- supply variable reference for variable 'A'
> say "a="pp(a)
> say
>
> call two <a -- supply variable reference for variable 'A'
> say "a="pp(a)
> say
>
> call three >a -- supply variable reference for variable 'A'
> say "a="pp(a)
> say
>
> b.1="value set in main program"
> b.0=1
> call four >b.
> do i=1 to b.0
> say "b."i"="pp(b.i)
> end
>
> ::routine one
> use arg <ref -- fetch as variable reference
> ref="this value comes from routine named 'one'"
>
> ::routine two
> use arg >a -- fetch as variable reference
> a="this value comes from routine named 'two'"
>
> ::routine three
> use arg varRef -- fetch variable reference object
> say "in three: varRef="pp(varRef~class) "varRef:" pp(varRef)
> varRef~value="this value comes from routine named 'three'"
>
> ::routine four
> use arg >x. -- fetch as variable reference
> x.2="value set in routine 'four'"
> x.0=2
>
> ::routine pp -- return string value enclosed in square brackets
> return "["arg(1)"]"
>
>
> Here the output when running the above program:
>
> a=[this value comes from the main program ('prolog')]
>
> a=[this value comes from routine named 'one']
>
> a=[this value comes from routine named 'two']
>
> in three: varRef=[The VariableReference class] varRef: [this value comes from
> routine named 'two']
> a=[this value comes from routine named 'three']
>
> b.1=[value set in main program]
> b.2=[value set in routine 'four']
>
> So supplying a variable reference as an argument to a routine only needs
> the "<" or ">" operator being prepended to the parameter.
>
> Using the argument as a variable alias is possible by merely prepending
> the "<" or ">" operator to the argument's local name in the USE ARG keyword
> statement as demonstrated in the routines named "ONE", "TWO" and "FOUR".
>
> If the variable reference argument is fetched without prepending the "<"
> or ">" operator, then the passed in variable reference object gets assigned
> to it as is demonstrated in routine "THREE". In this case the methods of
> the class VariableReference can be used and need to be used to assign a
> value to the variable of the caller.
>
> ---
>
> Routine 'FOUR' above demonstrates how to use a variable reference for a
> stem variable and using a local alias to the stem variable B. as well
> (under the local stem name X.), something - as Rick has pointed out -
> classic Rexx programmers would expect when first employing USE ARG in
> ooRexx.
>
Routine FOUR does not really show any advantage to using variable
references. The same program would produced the same result using a simple
USE ARG. The following version would function differently because of the
references:
::routine four
use arg >x. -- fetch as variable reference
x.="New default value" -- assign a new default value to the stem.
x.2="value set in routine 'four'"
x.0=2
With a straight USE ARG with a stem variable, the local variable is
assigned to the same stem object pointed to by the stem variable in the
caller. Assigning a new default value creates a new stem object to the
local variable, thus severing any connection between the local variable and
the caller's variable. If you are using a variable reference, a new stem
object is still created, but because the local variable is just a alias to
the referenced variable, it is the stem variable back in the caller that is
assigned the new value. Thus all of the changes get passed back.
Rick
> ---
> I think this is not only a "cool" or "nice" feature but it helps in
> certain circumstances Rexx programmers a lot! And the implementation seems
> to be easily understandable and applicable!
>
>
> ---rony
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Oorexx-devel mailing list
> Oorexx-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/oorexx-devel
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Oorexx-devel mailing list
Oorexx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/oorexx-devel