Polymorphic choices

Templates themselves cannot have type arguments, but how about choices? Can they be polymorphic? If so, what is the syntax for declaring the type variables?

Concretely, I’d like to write a DAML template that I can use to delegate fetching of contracts for arbitrary other templates. In the following attempt, I’m getting the error that the type variable a is not in scope.

template GenFetch with
    s: Party
    d: Party
  where
    signatory s

    controller d can
      nonconsuming GenericFetch: a
        with cid: ContractId a
        do
          fetch cid
1 Like

Choice arguments cannot be polymorphic. If you look at the DAML-LF specification at daml/daml-lf-1.rst at 9f43ab95ac6aa1be9134b7f8350f2f51969828e0 · digital-asset/daml · GitHub you can see in the typing rule for well-formed choices requires that the choice argument type is a serializable type in an empty context which in particular means that it must not have any free type variables.

What is different for choice arguments in DAML-LF is that the choice argument can be a type constructor applied to a type variable as long as the type variable is fixed, e.g., Optional Int would be a valid type for a choice argument. We made use of this fact when we still had generic templates (remember that even then templates were not allowed to have type arguments). However, these days you cannot write DAML code that will generate such a choice.

2 Likes