mutablenushell

Nushell table update works unless I set the result to a mutable variable


This code runs perfectly:

    $table | update col { |row| "some value" } | print

This code also runs perfectly:

    let $final_table = $table | update col { |row| "some value" }
    $final_table | print

This code throws a Command does not support nothing error on the call to update:

    $table = $table | update col { |row| "some value" }
    $table | print

The only difference in the last one is that $table (a mutable variable) is being set, instead of a newly created variable or just directly pipelining to the print command. Why would that make a difference to this code, erroring or not?


Solution

  • Since Nushell 0.83, declaration keywords such as let and mut no longer require the use of subexpressions to assign the output of a pipeline to a variable (see blog).

    However, assigning the output of a pipeline to a mutable variable as you are doing in this case is an intentional exception to this language improvement. For that, you'll still need to use subexpressions:

    $table = ($table | update col "some value")
    

    Note that I also simplified your update command here, as it only contained a static value.

    Given the language improvement, this could also be done using the let or mut keywords. For example:

    let $final_table = $table | update col { |row| "some value" }
    

    or

    mut $table = $table | update col { |row| "some value" }
    

    However, redeclaring (shadowing) the same variable is not a good practice. Best to continue to use subexpressions for this purpose, as demonstrated in the first example above.