Nix:nixlang 语法如何更改现有集?语法错误意外 '=' ,期待 $end

Nix: nixlang syntax how to change an existing set ? Syntax error unexpected '=' , expecting $end

nix repl
nix-repl> test = {"a"=10;}
nix-repl> test.a
nix-repl> 10
nix-repl> test.a=20
error: syntax error, unexpected '=' , expecting $end, at (string):1:7

预期结果:

test = {"a"=20;}

我目前正在学习 nix,google 几分钟后找不到答案。 这看起来很简单,我相信有人会立即知道这一点。

nix 中的值是不可变的;您不能更改 test.a 的值,因为那样需要更改集合。您只能创建具有不同 a 值的 new 集合。

nix-repl> test = {"a"=10;}

nix-repl> test // {"a"=20;}
{ a = 20; }

nix-repl> test
{ a = 10; }

nix-repl> test2 = test // {"a"=20;}

nix-repl> test2
{ a = 20; }

// 运算符组合两个集合,右侧的值覆盖左侧的值。

nix-repl> {"a"=10; "b"=20;} // {"a"="changed"; c="30";}
{ a = "changed"; b = 20; c = "30"; }