How to get the name of a template or data type

Hello,
Let’s say that I have the following template:

template Hello
  with
    owner: Party
  where
    signatory owner

How can I print only the template name once I create it in a script? Is there something similar like the Haskell’s typeOf function?

There is in the form of TypeRep, but the function to print those type representations is a dev-only feature, meaning it’s only available in Daml-LF 1.dev. Might be of use in a test script, but cannot be used in a package that you deploy to the ledger:

module Main where

import Daml.Script

template Hello
  with
    owner: Party
  where
    signatory owner

ttrFromCid : forall t . (Template t) => ContractId t -> TemplateTypeRep
ttrFromCid x = templateTypeRep @t

setup : Script ()
setup = script do
  owner <- allocatePartyWithHint "owner" (PartyIdHint "owner")

  helloCid <- submit owner do
    createCmd Hello with ..

  Some hello <- queryContractId owner helloCid

  let tr = ttrFromCid helloCid

  debug (templateTypeRepToText tr)

Requires in daml.yaml:

build-options:
  - --target=1.dev

You’ll get output (in the IDE):

Trace: 
  "-homePackageId-:Main:Hello"
3 Likes

Thanks @bernhard,
Why is this limited to dev only? I would like to have this not only in dev environment.

Adding stable features has ongoing cost as Daml has strong backwards compatibility guarantees. Whether this particular feature has enough value is not clear. We have opened a ticket here for discussion: Investigate impact/need of providing template names in Daml Script · Issue #19604 · digital-asset/daml · GitHub

1 Like