如何在jsonnet中附加现有对象?

mai*_*kel 4 jsonnet ksonnet

如何附加到现有列表?

这是无效的:

local list = ['a', 'b', 'c'];

local list = list + ['e'];
Run Code Online (Sandbox Code Playgroud)

sba*_*ski 6

您遇到的情况是由于本地人在 jsonnet 中递归。因此,local list = list + ['e']右侧列表中的列表与左侧列表中的列表相同,因此当您尝试对其求值时会导致无限递归。

因此,这将按您的预期工作:

local list = ['a', 'b', 'c'];
local list2 = list + ['e'];
Run Code Online (Sandbox Code Playgroud)

这次它正确地引用了先前定义的列表。

如果你想知道为什么它是这样设计的,它很有用,因为这意味着你可以编写递归函数:

local foo(x) = if x == 0 then [] else foo(x - 1) + [x];
foo(5)
Run Code Online (Sandbox Code Playgroud)

这与写作完全相同:

local foo = function(x) if x == 0 then [] else foo(x - 1) + [x];
foo(5)
Run Code Online (Sandbox Code Playgroud)