Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 23620

Scripting • Re: What does op type (>[ ... ]) do?

$
0
0
used this construct in script file to abbreviate a series of commands, expressions.
As I wrote, kind of embedded function, that can access local variables. It makes code shorter and more readable.
I guess I never thought about this, but it does make some sense...

The "locals in the calling scope " are available the script in the (>[...]). Since the outer (>...) returns a function, and the in the inner [] that returns a code data-type. So using the (>[]) function essentially becomes "inlined" (NOT pushed to the call stack), thus the local variables are in scope. While the do={} does create a new scope (and function pushed to call stack), so locals in a parent scope are NOT available.

Perhaps the more simple example:
Code:
# version 1{    :local x 1    :local fn do={:put $x}    $fn}# prints nothing, since $x is not in scope# version 2# vs, using (>[]) which creates a function that returns a code data-type, and prints "2"{    :local x2 2    :local fn2 (>[:put $x2])    $fn2}2
And you can see the RouterOS's "IL", between the two versions — one uses a "/localdo=" while another treat the (>) as just another local ("/localname=")
Code:
# version 1(evl / (evl /localname=$x;value=1) (evl /localdo=;(evl (evl /putmessage=$x));name=$fn) (<%% $fn (> $fn)))# version 2(evl / (evl /localname=$x2;value=2) (evl /localname=$fn2;value=(> (evl (evl /putmessage=$x2)))) (<%% $fn2 (> $fn2)))

Statistics: Posted by Amm0 — Sun Apr 06, 2025 8:25 pm



Viewing all articles
Browse latest Browse all 23620

Trending Articles