【问题标题】:Increment variable from methods从方法中增加变量
【发布时间】:2021-07-19 08:37:06
【问题描述】:

我想在方法中增加一个计数器,如下所示:

  row(title, height):: {
    _panels:: [],
    _panelSlot:: 0,

    addPanel(panel):: self {
      self._panelSlot = self._panelSlot + 1,
      _panels+: [{
          slot: panelSlot,
          panel: panel,
      }],
    },

self._panelSlot = self._panelSlot +1 行是我所期望的,但这确实会导致以下错误:

STATIC ERROR: lib/grafana/builder.libsonnet:157:7-11: unexpected: self while parsing field definition

我的第二次尝试:

我不确定这是否会覆盖 _panelSlot 变量,但我无法确认,因为我无法访问对象数组上的该变量:

  row(title, height):: {
    _panels:: [],
    // height is the row's height in grid numbers and it is used' to calculate the 
    // panel's height and y pos accordingly.
    _height:: height,
    // panelSlot is the row panels' current position from left-to-right. If there are
    // two or more stacked panels they share the same slot. This is used to calculate
    // the width for each panel.
    _panelSlot:: 0,

    addPanel(panel):: self {
      local parent = self,
      _panelSlot:: self._panelSlot + 1,
      _panels+: [{
          slot: parent._panelSlot,
          panel: panel,
      }],
    },
RUNTIME ERROR: max stack frames exceeded.
...
<anonymous>
        lib/grafana/builder.libsonnet:(13:9)-(15:10)    thunk <array_element>
        lib/grafana/builder.libsonnet:19:29-35  object <anonymous>
        lib/grafana/builder.libsonnet:19:15-37  thunk <array_element>
        lib/grafana/builder.libsonnet:(7:24)-(20:6)     object <anonymous>
        During manifestation

我的问题

如何修改方法中的变量(即递增计数器),以便在下次调用该方法时可以访问修改后的变量?

【问题讨论】:

    标签: jsonnet


    【解决方案1】:

    如何在方法中修改变量(即递增计数器),以便在下次调用该方法时可以访问修改后的变量?

    IIUC 不能以类似于其他 OOP 语言的方式真正保持和改变状态

    您可以通过 链接 调用 返回新对象强>

    示例1

    在下面制作一个示例:

    代码:

    local Array = {
      array: [],
      count: 0,
    
      addElem(e):: self {
        // Overload (mutate) this object
        array+: [e],
        count+: 1,
      },  // return mutated object
    };
    
    // Below 4 lines are equal to
    // Array.addElem('x').addElem('y').addElem('z'); but kinda more readable
    local obj = Array
                .addElem('x')
                .addElem('y')
                .addElem('z');
    
    obj
    

    输出:

    $ jsonnet example1.jsonnet
    {
       "array": [
          "x",
          "y",
          "z"
       ],
       "count": 3
    }
    

    Example2

    将上述方法应用于您的代码:

    代码:

    local Class = {
    
      Row(title, height):: {
        local this = self,  // clamp a reference to the current self
    
        _panelSlot: 0,
        _panels: [],
        _height: height,
    
        title: title,
    
        // addPanel() changes it-`self` to return a mutated Row
        addPanel(panel):: self {
          _panelSlot+: 1,
          _panels+: [{
            slot: this._panelSlot,
            panel: panel,
          }],
        },
      },
    };
    
    local obj = Class
                .Row('Title', 42)
                .addPanel('Foo')
                .addPanel('Bar');
    
    obj
    

    输出:

    $ jsonnet example2.jsonnet
    {
       "_height": 42,
       "_panelSlot": 2,
       "_panels": [
          {
             "panel": "Foo",
             "slot": 0
          },
          {
             "panel": "Bar",
             "slot": 1
          }
       ],
       "title": "Title"
    }
    

    顺便说一句,如果您还没有,建议您查看grafonnet-lib

    【讨论】:

    • 感谢您的回答,虽然我无法从我的方法中访问变量,但这并没有真正帮助我。关于 grafonnet-lib:我正在使用 grafonnet-lib 之上的构建器模式构建一个抽象层
    • @kentor 感谢您的反馈,精心制作的Example2(添加到我之前的答案中)可能有助于在您的代码上使用这种方法,请注意我更改了一些字段以体现出来(即:: -> :),为了显示“内部”数据状态。
    • @kentor 请注意对 Example2 的一些小(虽然漂亮;)更改,以获取更多惯用的 +: 代码围绕变异返回的 this obj 字段
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 2021-01-03
    相关资源
    最近更新 更多