Lesson 4: Template and Choices
Nothing is loaded from YouTube until you press play, and then without tracking cookies.
About this video
For a better learning experience, sign up and enroll in this free course at: daml.talentlms.com/plus/catalog/courses/150
Transcript
1,108 wordsCaptions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.
- 0:00
demo language consists of two parts the syntax as a functional programming language which previous lessons explored and the modeling language which is used for creating templates and contracts to interact with a ledger this lesson will focus on the modeling language aspect of damel template the blueprint of a contract is created in the scope of module to define a module use the module keyword followed by the name of the module
- 0:35
the where keyword is used to define the scope of the module as far as syntax defining a template is similar to defining a custom data type except you use the template keyword instead of the data keyword followed by the name of the template the reason is that defining a template does Define a custom record data type of the same name under the hood when a contract is created the create command takes a record value associated with a template
- 1:09
it is that value that gets persisted to The Ledger inside the width block is where to list all the parameters with their data types the with block is where you define what information your contract payload will carry once a contract is Created from the template the where block is the body of the template where you specify the roles of different parties as well as preconditions
- 1:36
unlike a record a template must have a signatory having a signatory is what separates a template from a record a party can be assigned as an observer to have the authority to view a contract to declare a party to be a controller they must be explicitly designated as an observer beforehand unlike signatory contract key is optional key can be of any data type in this example the key is username which is a party however when there are multiple attributes in a key
- 2:12
typically you use a tuple what key indicates is the uniqueness of each contract in this example the key is username this indicates that there can only be one social network user contract per each unique username field if there is more than one key such as username and Alias then each contract will be unique within the scope of the compound key username and alias that means there
- 2:41
can be several contracts with the same username as long as all the aliases associated with this username are unique it's important to note that one of the keys must be a signatory contract Keys have an Associated set of key maintainer parties these parties guarantee the uniqueness of their maintained keys if you specify a key you must also specify a maintainer maintainer can take several parties but they all must be signatories and keys of the contract
- 3:15
the ensure function is used to define preconditions of contract Creation in this example the precondition ensures that the Alias field cannot be an empty string if a party attempts to create a contract by passing an empty string for the Alias field the contract will not be created a choice is an action that a participant can take on a contract which is an instance of a template
- 3:47
defining a choice begins with the choice keyword followed by the name of the choice and the return type in the example the choice follow returns contract ID social network user indicating that the choice returns a contract ID of a social network user contract it is important to note that choice by default returns an update syntactic sugar allows you to Omit writing update for the
- 4:15
choice return type if the keyword non-consuming is omitted a choice is consuming by default this means that the contract is archived before the body of the exercise where the do block begins is executed this archival behavior is analogous to the pre-consuming behavior you can add the keyword pre-consuming in front of the choice keyword in the same way that the non-consuming keyword is added
- 4:46
just like the default Choice when a pre-consuming choice is exercised the contract is archived before the body of the choice is executed you can think of a pre-consuming choice or a default Choice as a non-consuming choice that implicitly archives the contract before anything else happens unlike a non-consuming choice the contract cannot be fetched anywhere in the body of the choice
- 5:12
since pre-consuming choice archives the contract implicitly before the body of the choice is run a post-consuming choice archives the contract after the body of the choice is executed a post-consuming choice can be thought as a non-consuming choice that implicitly archives the contract after the choice has been exercised for the purpose of this demo a post-consuming choice won't work since it violates the uniqueness of the key as a reminder you defined the key and
- 5:47
maintainer in the body of the template to ensure that there could only be one social network user contract associated with a particular party that means in order to create a new contract you must archive the old one to avoid running into the unique key violation error in the body of the follow Choice the create function is called to create a new Social Network user contract since
- 6:14
a post-consuming choice archives the contract only after the body of the choices run you will run into an error saying that you can't create two contracts with the same key to have better control over when a contract gets archived instead of using pre-consuming or post-consuming keywords use the non-consuming keyword to call archive self wherever you need to within the body of the choice
- 6:41
in fact pre-consuming and post-consuming are shorthands for non-consuming with archive for this demo if you decided to convert the default Choice into a non-consuming choice you would have to make sure to Archive self somewhere before the create expression to avoid the unique key violation error the next lesson will test the demo code to see what the error looks like and discuss how to handle it this lesson reviewed the basic template structure and explained how
- 7:14
the keywords pre-consuming and post-consuming can be used compared to the default choice and non-consuming choice the archiving behavior of a pre-consuming choice is analogous to the default Choice as both types of choices implicitly archive a contract before the body of the choice is run on the other hand post-consuming Choice implicitly archives a contract after running the body of the
- 7:41
choice to have better control over the archiving behavior of a choice you can define a choice with the non-consuming keyword and archive it with the Arca function within the body of the choice