Input limit on template Key

Hello team,

Is there any inout limit on template key? it seems like more than 5 inputs doesn’t work and give me error.

Example template:

template Example
  with
    party1: Party
    party2: Party
    party3: Party
    id1: Text
    id2: Text
    id3: Text
  where
    signatory party1
    
    key (party1, party2, party3, id1, id2, id3) : (Party, Party, Party, Text, Text,Text)
    maintainer key._1

Error message:

    • Ambiguous type variable ‘a0’ arising from a use of ‘toParties’
      prevents the constraint ‘(IsParties a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance IsParties Party
          -- Defined in ‘DA.Internal.Template.Functions’
        instance IsParties (Optional Party)
          -- Defined in ‘DA.Internal.Template.Functions’
        instance IsParties [Party]
          -- Defined in ‘DA.Internal.Template.Functions’
        ...plus two instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the expression:
        toParties ((DA.Internal.Record.getField @"_1" key))
      In an equation for ‘_maintainer’:
          _maintainer _ key
            = toParties ((DA.Internal.Record.getField @"_1" key))
            where
                _ = key
      In the instance declaration for
        ‘HasMaintainer Example (Party, Party, Party, Text, Text, Text)’
```Preformatted text

thank you,
Dorrit
2 Likes

By the way, could you copy and paste the error message you were getting too? That would be helpful

This is unrelated to keys, the issue is the _1 selector which is only defined for tuples up to length 5. If you need more, you have 3 main options:

  1. Define your own record type instead of using tuples. This can be a lot easier to understand since you can give sensible names to the fields instead of just relying on numbers. So this is the option I’d usually recommend. For this simple example you don’t actually need a separate key type since your key includes all fields from Example but given that’s usually not the case, I’ve defined a separate type in the example to see how it works in general.
    Full example:
data ExampleKey = ExampleKey with
  party1 : Party
  party2 : Party
  party3 : Party
  id1 : Text
  id2 : Text
  id3 : Text

template Example
  with
    party1: Party
    party2: Party
    party3: Party
    id1: Text
    id2: Text
    id3: Text
  where
    signatory party1

    key ExampleKey party1 party2 party3 id1 id2 id3 : ExampleKey
    maintainer key.party1
  1. Group your tuple into smaller tuples, e.g., one for parties and one for the text fields.
template Example
  with
    party1: Party
    party2: Party
    party3: Party
    id1: Text
    id2: Text
    id3: Text
  where
    signatory party1

    key ((party1, party2, party3), (id1, id2, id3)) : ((Party, Party, Party), (Text, Text, Text))
    maintainer key._1._1
  1. Use pattern matching instead of selecting via _1
template Example
  with
    party1: Party
    party2: Party
    party3: Party
    id1: Text
    id2: Text
    id3: Text
  where
    signatory party1

    key (party1, party2, party3, id1, id2, id3) : (Party, Party, Party, Text, Text, Text)
    maintainer case key of (p, _, _, _, _, _) -> p

Hope that helps!

6 Likes

Well explained! thank you so much!

1 Like

Would be really great if these were added to the docs on Reference: Contract Keys — Daml SDK 2.2.0 documentation

1 Like