Why do templates need a Show instance?

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
1 Like

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?

1 Like

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.

1 Like

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.

2 Likes

@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?