2020-02-18 15:44:31 +01:00
|
|
|
# Local Variables
|
|
|
|
|
|
|
|
RETRO does not provide local variables. Similar functionality
|
|
|
|
can be achieved using globals along with the `v:preserve`
|
|
|
|
combinator, but this can lead to ugly code.
|
|
|
|
|
|
|
|
Consider:
|
|
|
|
|
|
|
|
'Counter var
|
2020-02-20 21:48:29 +01:00
|
|
|
:average (...n-v)
|
2020-02-18 15:44:31 +01:00
|
|
|
&Counter [ #0 !Counter
|
|
|
|
[ + &Counter v:inc ] times @Counter / ] v:preserve ;
|
|
|
|
:run-tests
|
|
|
|
#0 !Counter
|
2020-02-20 21:48:29 +01:00
|
|
|
#1 #2 #3 #4 #3 average n:put nl &Counter v:inc
|
|
|
|
#10 #20 #30 #40 #3 average n:put nl &Counter v:inc
|
|
|
|
#11 #21 #31 #41 #3 average n:put nl &Counter v:inc
|
2020-02-18 15:44:31 +01:00
|
|
|
@Counter n:put sp '_tests_finished s:put nl ;
|
|
|
|
|
|
|
|
run-tests
|
|
|
|
|
|
|
|
The code implemented here allows for wrapping the most recent
|
|
|
|
word within a `v:preserve` clause, letting the above definition
|
|
|
|
of `test` become:
|
|
|
|
|
2020-02-20 21:48:29 +01:00
|
|
|
:average (...n-v)
|
2020-02-18 15:44:31 +01:00
|
|
|
#0 !Counter [ + &Counter v:inc ] times @Counter / ;
|
|
|
|
&Counter make-local
|
|
|
|
|
|
|
|
And if you need multiple variables to be localized:
|
|
|
|
|
2020-02-20 21:48:29 +01:00
|
|
|
:average ... ;
|
2020-02-18 15:44:31 +01:00
|
|
|
{ &Array &Of &Variables } locals
|
|
|
|
|
|
|
|
# The Code
|
|
|
|
|
|
|
|
~~~
|
|
|
|
:make-local (a-)
|
|
|
|
here [ compile:lit
|
|
|
|
d:last<xt> compile:lit
|
|
|
|
&v:preserve compile:call
|
|
|
|
compile:ret ] dip d:last d:xt store ;
|
|
|
|
|
|
|
|
:locals (a-) &make-local a:for-each ;
|
|
|
|
~~~
|
|
|
|
|