【问题标题】:How to append an existing object in jsonnet?如何在 jsonnet 中附加现有对象?
【发布时间】:2018-09-27 06:00:52
【问题描述】:

如何追加到现有列表?

这是无效的:

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

local list = list + ['e'];

【问题讨论】:

    标签: jsonnet ksonnet


    【解决方案1】:

    您所经历的是由于本地人在 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)
    

    【讨论】:

    • 谢谢。现在我明白了函数式方法不适用于 jsonnet。
    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2014-07-04
    • 2012-01-09
    相关资源
    最近更新 更多