使用getTime函数时遇到问题 [英] Trouble using the getTime function

查看:155
本文介绍了使用getTime函数时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在合同选择的assert语句中使用getTime,如下所示:

I am using getTime inside an assert statement on a contract choice as follows:

Add_Car : CarId
        with
            startCoverage: Time
        do
          -- Check for a legal start date
          assert (
            startCoverage > getTime
            )
          create this with datetime_vehicle_added = startCoverage, covered=True

它会产生一个错误:

error:
    * Couldn't match expected type `Time' with actual type `m0 Time'
    * In the second argument of `(>)', namely `getTime'
      In the first argument of `assert', namely
        `(startCoverage > getTime)'
      In a stmt of a 'do' block: assert (startCoverage > getTime)

getTime是否不返回"Time"类型的值?什么是"mo时间"?

Does getTime not return a value of type 'Time'? what is 'mo Time'?

推荐答案

tldr:对于@bame,您需要将getTime的结果绑定到Update中 或Scenario do-block.即.

tldr: Per @bame, you need to bind the result of getTime in an Update or Scenario do-block. ie.

Add_Car : CarId
  with
    startCoverage: Time
  do
  -- Check for a legal start date
  now <- getTime
  assert $ startCoverage > now
  create this with datetime_vehicle_added = startCoverage, covered=True

要了解此处发生的情况,我们需要从以下类型开始 getTime:

To understand what has happened here we need to start with the type of getTime:

getTime : (HasTime m) => m Time

您期望的函数的类型签名是以下之一:

The type signature of the function you were expecting is one of:

getTimeValue : Time

getTimeFunc : () -> Time

要了解差异,您需要考虑纯度的概念 和封装.

To understand the difference you need to consider the concepts of purity and encapsulation.

在DAML中,所有功能都是纯函数.

In DAML all functions are pure.

纯函数是可以完全用以下形式描述的函数: 作为参数传递的值与作为参数返回的值之间的映射 结果.值是具体的东西,例如时间,整数,文本等,以及列表, 记录,值的变体以及我将要了解的其他一些内容 之后. getTimeValue是一个值,因此根据定义它是一个常数,其中 只是停止时钟"意义上的当前时间".

A pure function is a function which can be described completely in terms of a mapping between values passed as arguments, to values returned as a result. Values are concrete things such as Time, Int, Text, etc, and Lists, Records, and Variants of values as well as a few other things I'll get to later. getTimeValue is a value, so it is by definition a constant, which will only be the "current time" in the "stopped clock" sense.

getTimeFunc是一个采用类型为Unit的参数的函数,其中 表示您可以传递给它一个参数:().因为 函数是纯函数,这意味着它不能考虑其函数之外的任何事物 参数,因此此函数还必须返回一个常数.实际上 getTimeValuegetTimeFunc之间的唯一区别是您必须 传递getTimeFunc ()以获得常量.

getTimeFunc is a function that takes an argument of type Unit, which means there is exactly one argument you can pass to it: (). Because the function is pure, this means it cannot consider anything outside of its argument, so this function must also return a constant value. In fact the only difference between getTimeValue and getTimeFunc is that you must pass getTimeFunc () to get the constant.

存在一个带有当前时间"概念的外部世界, 可以讯问和使用的是上下文",这意味着使用 在输入->输出方面,这不再完全描述. 这被描述为不纯".

That there is an outside world with a concept of "current time" that you can interrogate and use is a "Context" that means any function that uses this can no longer be described completely in terms of input -> output. This is described as "impure".

在DAML中,所有功能都是纯函数,因此,如果我们要处理杂质",我们有 将杂质封装成纯净值.在DAML中,我们表达了这一点 封装类型:

In DAML all functions are pure, so if we want to handle "impurity" we have to encapsulate the impurity in a pure value. In DAML we express this encapsuation as a type thus:

encapsulatedImpureValue : m a

所以在我们的例子中,值是Time值:

so in our case, where the value is a Time value :

encapsulatedImpureTimeValue : m Time

您可以将其读取为类型为Time的封装值,具体取决于 要评估的上下文m.由于我们还没有提及有关 上下文m除了存在之外,还不足以让我们 实施它.具体来说,我们还需要说上下文必须是 一个带有当前时间"的概念,这就是我们最终得到的结果 DAML标准库中getTime的签名:

You can read this as, an encapsulated value of type Time that depends on a context m to evaluate. As we have not mentioned anything about the context m besides that it exists this is not quite enough to allow us to implement it. Specifically, we need to also say that the context has to be one with a concept of "the current time", which is how we end up with the signature of getTime in the DAML standard library:

getTime : (HasTime m) => m Time

您可以这样解读:时间Time的封装值取决于 支持HasTime的上下文m(即当前时间"的概念).

Which you can read as: an encapsulated value of time Time that depends on a context m that supports HasTime (ie. the concept of "current time").

我们现在可以写:

let now = getTime

now将是纯封装的值-不会立即 很有用,因为任何在期望纯Time的函数中使用它的尝试 值将失败,因为这将需要破坏封装和DAML 严格将封装违规作为编译错误.

and now will be a pure encapsulated value — which is not immediately useful, as any attempt to use it in any function expecting a pure Time value will fail, as that would require breaking the encapsulation, and DAML stricly enforces encapsulation violations as compilation errors.

要使用封装的值,必须首先指定合适的上下文,并且 然后运行该上下文中的值. DAML提供了两个上下文 支持HasTime:UpdateScenario.它还提供了一种运行方式 Scenario包装值和一种运行Update包装值的方法, 以及将Update值转换为Scenario值的两种方法.

To use an encapsulated value you must first specify a suitable context, and then run the value within that context. DAML provides two contexts that support HasTime: Update and Scenario. It also provides one way to run a Scenario wrapped values, and one way to run Update wrapped values, and two ways to convert Update values into Scenario values.

  1. DAML模块中的每个顶级方案值将由DAML运行 解释程序作为DAML测试.

  1. Each toplevel Scenario value in a DAML module will be run by the DAML interpreter as a DAML test.

每个DAML模板选择的主体都定义为Update值 行使选择权时将运行该程序.

The body of each DAML Template Choice is defined to be an Update value that will be run when the choice is exercised.

您可以使用submitsubmitMustFail函数生成一个 Scenario值,该值在运行时将运行已授权为的Update值 提名的Party.

You can use the submit, and submitMustFail functions to produce a Scenario value that, when run, will run an Update value authorized as the nominated Party.

组成封装值

几乎所有通用的都有许多标准API 用于将封装的值组成复合词的功能语言 价值观.您将听到最著名的:"Functor"和"Monad" 这些定义采用封装值和函数的函数,以及 以各种方式将它们结合起来.封装是这样的基础软件 工程原理,大多数FP都不足为奇 语言提供了语法糖,使使用这些语法糖更容易-DAML是 没什么.

Composing Encapsulated Values

There are a number of standard APIs that are common to almost all functional languages for composing encapsulated values into compound values. You will have heard of the most famous: "Functor" and "Monad" These define functions that take encapsulated values and functions and combine them in various ways. Encapsulation is such a fundamental software engineering principle that it shouldn't be surprising that most FP languages provide syntactic sugar to make using these easier — and DAML is no different.

作为Functor接口实例的封装值支持 fmap函数,DAML也为此函数提供了它作为中缀运算符<$>.

An encapsulated value that is an instance of the Functor interface supports the fmap function, for which DAML also provides as an infix operator <$>.

一个封装值,它是Monad接口的一个实例(称为 DAML中的Action),支持fmappure和bind/flatMap函数. DAML提供return作为pure的别名;和>>=运算符 绑定/flatMap.它还为>>=提供了do-notation作为语法糖,所以:

An encapsulated value that is an instance of the Monad interface (called Action in DAML), supports the fmap, pure, and bind/flatMap functions. DAML provides return as an alias for pure; and the >>= operator for bind/flatMap. It also provides do-notation as syntactic sugar for >>= so:

do
  t <- getTime
  a <- useTime t
  combineWithTime a t

生成一个复合Update值,该值(运行时)运行getTime, 将结果值传递给useTime,然后将两个结果都传递给 combineWithTime.此do-block的结果也封装为Update值, 这样我们就不会破坏封装,因为在我们运行的时候 updateA/B/C我们已经为封装提供了封装上下文 复合Update值.

Produces a compound Update value that (when it runs), runs getTime, passes the resulting value to useTime then passes both results to combineWithTime. The result of this do-block is also encapsulated Update value, so we don't break encapsulation because by the time we are running updateA/B/C we have provided the encapsulation context to the enclosing compound Update value.

如果(如您在示例中所做的那样),我们使此do阻止 选择,然后执行选择将运行复合更新. 另外,如果我们将其传递给submit,则可以将其作为 设想.如果您都不做(例如,如果您有 两个更新值,并使用if表达式在它们之间进行选择),然后 它不会产生可观察的效果,因为在DAML中,所有功能都是纯函数.

If (as you do in your example) we make this do block the body of a choice, then exercising the choice will run the compound update. Alternatively, if we pass it to submit we can run it as a part of a scenario. If you do neither (which can happen if, for instance, you have two Update values, and use an if expression to choose between them), then it will have no observable effect, because in DAML all functions are pure.

这篇关于使用getTime函数时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆