Skip to content
Discussions/App Development/Why do templates need a Show instance?Forum ↗

Why do templates need a Show instance?

App Development5 posts262 views5 likesLast activity Jan 2022
KR
Krisztian_PinterOP
Nov 2021

Why do templates need to be able to derive a Show instance? What is it used for exactly? I tried looking this up in the docs but I could not find an answer.

Also, why don’t keys need a Show instance? This works:

data Foo = Foo Int
-- no Show instance for Foo

template Bar
  with
    p : Party
    x : Int
  where
    key (p, Foo x) : (Party, Foo)
    maintainer key._1
    signatory p
CO
cocreature
Nov 2021

There is no super strict reason why we need one. It is currently used in the exception you get for failed ensure clauses for LF >= 1.14 but that’s a relatively recent addition and we had the show requirement for ages. There are two main reasons why we have it:

  1. Templates are by definition always serializable. That means that they can have a Show and Eq instance since things like functions are not possible.
  2. The second one is more of an ease of use thing. Most developers are used to basically everything having a toString and equals method. So my autoderiving these instances for templates, you don’t have to worry about understanding deriving just as you’re getting started and instead things just work™.

In your example, why don’t you want to add a Show and Eq instance to Foo?

KR
Krisztian_Pinter
Nov 2021

Thank you for the answer!

I was just wondering if the Show instance is used behind the scenes by anything specifically.

Adding a Show/Eq instance is not a problem in my example, I was just refactoring some code and when I added a new field to a template with a type that previously only appeared only in the key (which would be Foo in my example) I got an error. I was surprised because I assumed that it must have had a Show instance already. I was just wondering if there is a specific reason for keys not requiring a Show instance because this feels inconsistent to me.

CO
cocreature
Nov 2021
Krisztian_Pinter:

Adding a Show / Eq instance is not a problem in my example, I was just refactoring some code and when I added a new field to a template with a type that previously only appeared only in the key (which would be Foo in my example) I got an error. I was surprised because I assumed that it must have had a Show instance already. I was just wondering if there is a specific reason for keys not requiring a Show instance because this feels inconsistent to me.

keys are a bit different in that defining a key on a template does not define a new datatype. I think the distinction starts becoming clearer once you move from "why do templates require Show” to “why do templates autoderive Show”:

For a template autoderiving Show is nice since users don’t have to worry about understanding deriving. For a key we actually have the opposite: Given that it’s not a new datatype, we cannot autoderive the instance at the template definition. We could require it but then we force people to have to understand deriving (or manually define the instance) fairly early.

BE
Ben_M
Jan 2022
Krisztian_Pinter:

I tried looking this up in the docs but I could not find an answer.

@cocreature Thank you for those great responses, it makes sense, however as @Krisztian_Pinter said he could not find an answer.

From the perspective of building really comprehensive documentation for both Individuals and supported Customers, shouldn’t there be a definitive answer?

← Back to Discussions