Why do I need to indent so much when constructing (not creating) a contract?

Mod note: As of SDK 1.5.0 Scenarios have been superseded by the more powerful Daml Script. We now recommend using that for all purposes. For more information, and to learn how to use Script please check out @Andreas’ post on our blog.

I seem to need at least 5 spaces when indenting a with block in constructing a contract.

For example:

template Person
  with
    owner : Party
    name : Text
    dateOfBirth : Date
  where
    signatory owner

test = scenario do
  alice <- getParty "Alice"
  let person = Person with
        owner = alice
        name = "Alice"
        dateOfBirth = date 2020 Jan 1

  person.name === "Alice"

If I dedent the with block, so it looks like this:

  let person = Person with
      owner = alice
      name = "Alice"
      dateOfBirth = date 2020 Jan 1

Then I get the following error:

    • Constructor ‘Person’ does not have the required strict field(s): owner,
                                                                       name, dateOfBirth
    • In the expression: Person {}
      In an equation for ‘person’: person = Person {}

It appears as if the parser is recognizing nothing inside the with block.

If I go down to 2 spaces, I get an error on the first =:

    parse error on input ‘=’

Why do I need so much whitespace?

4 Likes

You need to indent further than the name of the variable you are defining in your let. In

let person = Person with
    owner = alice

you are defining 2 let-bindings, person is bound to Person with (so no fields which is why you are getting the error) and owner is bound to alice.

3 Likes

Thanks @cocreature! I didn’t realize I could define multiple values in a single let.

1 Like