【发布时间】:2018-09-27 06:00:52
【问题描述】:
如何追加到现有列表?
这是无效的:
local list = ['a', 'b', 'c'];
local list = list + ['e'];
【问题讨论】:
如何追加到现有列表?
这是无效的:
local list = ['a', 'b', 'c'];
local list = list + ['e'];
【问题讨论】:
您所经历的是由于本地人在 jsonnet 中递归。因此在local list = list + ['e'] 中,右侧的列表与左侧的列表相同,当您尝试对其求值时会导致无限递归。
所以这会像你期望的那样工作:
local list = ['a', 'b', 'c'];
local list2 = list + ['e'];
这一次它正确地引用了之前定义的列表。
如果你想知道为什么要这样设计,它很有用,因为意味着你可以编写递归函数:
local foo(x) = if x == 0 then [] else foo(x - 1) + [x];
foo(5)
这和写的一模一样:
local foo = function(x) if x == 0 then [] else foo(x - 1) + [x];
foo(5)
【讨论】: