gpt4 book ai didi

jsonnet - 将对象与 +(加号运算符)与空白组合

转载 作者:行者123 更新时间:2023-12-05 04:58:54 37 4
gpt4 key购买 nike

Jsonnet 的文档提到 + 运算符可用于 inheritance。 ,或者,如教程中所述,到 combine objects :

{
a: 1,
b: 2,
}
+
{
a: 3
}

但是,我注意到 - 至少在像上面这样的简单情况下 - 简单地省略 + 运算符并写入由空格分隔的两个连续对象编译为相同的结果。也就是说,上面的程序和这个程序的输出没有区别:

{
a: 1,
b: 2,
}
{
a: 3
}

我很困惑,因为我没有注意到文档中有任何提及这种隐式对象组合。我还注意到这种行为似乎是对象独有的,其他类型不会发生。 (特别是,尽管 Jsonnet 的某些功能从 Python 中汲取了灵感,但您不能像在 Python 中那样隐式地将字符串与空格连接起来。)

因此,一些问题:

  • 这是有意为之的行为还是错误?
  • 是否在任何地方记录了它?
  • 使用显式 + 运算符组合对象与使用空格隐式组合对象之间是否存在任何行为差异?

最佳答案

一位同事建议我在教程中提及这一点,在 Object Orientation 中部分:

Let's make it more concrete by mixing deriving some cocktails that are quite similar from a template that draws out their similarities. The + operator is actually implicit in these examples. In the common case where you write foo + { ... }, i.e. the + is immediately followed by a {, then the + can be elided. Try explicitly adding the + in the 4 cases, below.

local templates = import 'templates.libsonnet';

{
// The template requires us to override
// the 'spirit'.
'Whiskey Sour': templates.Sour {
spirit: 'Whiskey',
},

// Specialize it further.
'Deluxe Sour': self['Whiskey Sour'] {
// Don't replace the whole sweetner,
// just change 'kind' within it.
sweetener+: { kind: 'Gomme Syrup' },
},

Daiquiri: templates.Sour {
spirit: 'Banks 7 Rum',
citrus+: { kind: 'Lime' },
// Any field can be overridden.
garnish: 'Lime wedge',
},

"Nor'Easter": templates.Sour {
spirit: 'Whiskey',
citrus: { kind: 'Lime', qty: 0.5 },
sweetener+: { kind: 'Maple Syrup' },
// +: Can also add to a list.
ingredients+: [
{ kind: 'Ginger Beer', qty: 1 },
],
},
}

因此,隐式对象组合:

  • 是合法的
  • 有记录,但似乎只在教程和 formal spec 中,目前,不在语言引用中
  • 具有与 + 相同的行为,除了它仅在第二个操作数以 { 开头时可用,而不是在它是变量时可用。

关于jsonnet - 将对象与 +(加号运算符)与空白组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63792401/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com