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

问题描述 投票:0回答:1
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
1个回答
0
投票

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"; }
© www.soinside.com 2019 - 2024. All rights reserved.