处理模板中的记录

时间:2020-03-30 00:00:25

标签: daml

我想对代码进行一些结构化,并在模板中引入记录。使用简单版本,尽管记录不起作用,但它可以正常工作。你能建议吗? 谢谢

-工作版本-

daml 1.2
module Magic_potion where

template Potion_Taken
  with
    holder : Party
    tube_id : Text
  where
    signatory holder

    controller holder can
      UpdateData : ContractId Potion_Taken
        with
          newTube_id : Text
        do
          create this with
            tube_id = newTube_id

sampling_1 = scenario do
  w <- getParty "Wizard 1"
  potionTaken <- submit w do create Potion_Taken with holder = w; tube_id = "tube 1"
  newPotionTaken <- submit w do exercise potionTaken UpdateData with newTube_id = "tube 2"
  submit w do 
    newP <- fetch newPotionTaken
    assert (newP.tube_id == "tube 2")

-故障版本-

data Content = Content
  with
    tube_id : Text

template Potion_Taken
  with
    holder : Party
    content : Content
  where
    signatory holder

    controller holder can
      UpdateData : ContractId Potion_Taken
        with
          newTube_id : Text
        do
          create this with 
            content.tube_id = newTube_id -- This line seems to be the trouble

sampling_1 = scenario do
  w <- getParty "Wizard 1"
  potionTaken <- submit w do create Potion_Taken with holder = h; content = Content with tube_id = "tube 1"
  newPotionTaken <- submit h do exercise potionTaken UpdateData with newTube_id = "tube 2"
  submit w do 
    newB <- fetch newPotionTaken
    assert (newP.content.tube_id == "tube 2")

1 个答案:

答案 0 :(得分:1)

create this with 
  content.tube_id = newTube_id -- This line seems to be the trouble

这确实是您的问题。为避免歧义,with子句仅允许单级命名。这意味着如果您希望在Content记录中添加新的Potion_Taken记录,则需要创建一个。幸运的是with干净地嵌套在一起,所以不太麻烦。

create this with
  content = Content with tube_id = newTube_id

此外,如果Content有多个字段,而您只想更新一个子集,则复制构造函数语法也可以在这里使用:

create this with
  content = this.content with tube_id = newTube_id