You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
it is somehow a special variable in closure (since 0.92) #12690
changed the title [-]`it` is somehow a special variable in closure (in 0.92)[/-][+]`it` is somehow a special variable in closure (since 0.92)[/+]on Apr 28, 2024
In do_.rs in the nu-cmd-lang crate, we get a reference to the do block with the following line (l. 78 I think):
let block = engine_state.get_block(block.block_id);
With a debug print, we see that somehow, the block has a positional argument in its signature when $it is declared and referenced inside. If I add a positional parameter like so
do {|x| print$x; let$it=2; print$it} 5
it works as expected (it prints 5, then 2) and the block only has one positional argument x
This behavior seems to come parser.rs:4451 in nu-parser. If a signature already exists (like the |x| above), it takes it, but if not, it creates one if $it is in the block, not sure why. Could we get rid of the else block there?
Activity
[-]`it` is somehow a special variable in closure (in 0.92)[/-][+]`it` is somehow a special variable in closure (since 0.92)[/+]dedebenui commentedon Apr 29, 2024
In
do_.rsin thenu-cmd-langcrate, we get a reference to thedoblock with the following line (l. 78 I think):With a debug print, we see that somehow, the block has a positional argument in its signature when
$itis declared and referenced inside. If I add a positional parameter like soit works as expected (it prints 5, then 2) and the block only has one positional argument
xThis behavior seems to come
parser.rs:4451innu-parser. If a signature already exists (like the|x|above), it takes it, but if not, it creates one if$itis in the block, not sure why. Could we get rid of theelseblock there?WindSoilder commentedon May 15, 2024
@dedebenui Excellent hack! I think nushell once allowed such syntax before(yeah version 0.44):
In this way we need to run
do $xwith additional argument. But it's not allowed anymore, so I think we can get rid ofelseblock there.allow define it as a variable inside closure (#12888)
allow define it as a variable inside closure (nushell#12888)