预构建模块绑定

时间:2019-05-04 06:17:14

标签: pact-lang

我是Pact(Kadena.io)的新手,能否请您简单介绍一下模块绑定的作用?手册中的解释尚不完全清楚。

谢谢。

pact-lang

1 个答案:

答案 0 :(得分:2)

The bind function takes a row object as an input and allows you to associate variable names with the values associated with the keys of that object. For example, suppose I have the following object:

{ "foo" : "is"
, "bar" : 1
, "baz" : 3
}

When I call bind on this object, I can associate the values in of the object with a variable name that I specify and subsequently use those variables in expressions within the binding scope. Consider the following example:

(let ((obj { "foo": "is", "bar": 1, "baz": 3 }))
  (bind obj 
    { "foo" := foo
    , "bar" := bar
    , "baz" := baz
    }
    (format "1 + 3 {} {}" [foo (+ bar baz)]))
  )

This will output the string "1 + 3 is 4", by binding our own variable name definitions and using them in a formatting expression that returns a string. Notice what we've done here though. We've taken the values associated with the keys of the object obj, and bound them to a variable name, which we can work with in the scope of the binding function. This allows us to work with objects in a robust way. It even supports partial bindings on just parts of the object!

So the key takeaway here for the bind function is this:

  • When you work with objects, bind allows you to work with the values in the object as variable names.

  • The symbol := is this symbol that allows you to bind a key to a variable name. You may encounter this symbol with other functions, such as with-read and resume, and it always refers to the act of binding a value to some name.

  • You may use the variables for whatever purpose as long as they do not escape the binding scope.

I hope that clears things up.