# Implementing a wrapper template

**URL:** <https://forum.canton.network/t/implementing-a-wrapper-template/3421>\
**Category:** App Development\
**Created:** [November 9, 2021, 3:30pm UTC](https://forum.canton.network/t/implementing-a-wrapper-template/3421 "2021-11-09T15:30:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asarpeshkar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/asarpeshkar/32/1601_2.png) [@asarpeshkar](https://forum.canton.network/u/asarpeshkar)\
**Post date:** [November 9, 2021, 3:30pm UTC](https://forum.canton.network/t/implementing-a-wrapper-template/3421/1 "2021-11-09T15:30:01Z")

</div>

I’d like to implement a template that wraps the choices of another template, where exercising a wrapped choice on the wrapper exercises the underlying choice, together with a hook that updates the wrapper.

I can write the following type-class:

```haskell
class Choice t c r => WatchesChoice t c r a where
  onExercise : t -> c -> r -> a -> Update a

```

which lets me define the hook. Then, given the following type:

```haskell
-- r is here to satisfy the coverage condition
data Watched t c r = Watched with
  id : ContractId t
  choice : c

```

I’d like to define the following:

```haskell
instance (Template a, WatchesChoice t c r a) => HasExercise a (Watched t c r) (a, r) where
  exercise aId (Watched tId c) = do
    a <- fetch aId
    r <- Prelude.exercise tId c
    return (a, r)

```

This compiles, but I don’t think it’s actually usable as there’s no way to declare the controller on the choice via `HasExercise`, and there’s no way to satisfy the `Choice` constraints (which are needed to exercise this thing from a script) since I can’t, AFAIK, implement `ToAnyChoice` and `FromAnyChoice` myself.

I’m pretty sure this omission is by design, because allowing templates to be open to extension in this way would violate the principle of knowing what you’re signing.

Given that, is there any way to approximate what I want Daml today?

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [November 9, 2021, 4:29pm UTC](https://forum.canton.network/t/implementing-a-wrapper-template/3421/2 "2021-11-09T16:29:46Z")

</div>

You can’t add new choices to a template after the fact, for the reasons you mentioned yourself. You can write your own function `wrappedExercise` that does what you want, but you can’t call that directly via the API.

With the Daml Interfaces under development, you may be able to write a template with a choice `WrappedExercise` that takes a contract id and `Watched` and then does the magic you want. You could call that via a `createAndExercise` command via the API.

It would be good to know whether that works, or where Daml Interfaces in their current state fall short for that use case.

---

<div class="post-metadata">

**Author:** ![asarpeshkar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/asarpeshkar/32/1601_2.png) [@asarpeshkar](https://forum.canton.network/u/asarpeshkar)\
**Post date:** [November 9, 2021, 5:29pm UTC](https://forum.canton.network/t/implementing-a-wrapper-template/3421/3 "2021-11-09T17:29:08Z")

</div>

Daml interfaces solve this general problem, as I can create a `Hook` interface with a `onExercise : AnyChoice -> Update ()` choice.

However the lack of parameterization imposes a minor inconvenience in that I now have to unpack the `AnyChoice` at runtime, and the compiler can’t check my implementation for exhaustiveness.

Returning an arbitrary update is also potentially dangerous, but I know there’s work ongoing to figure out how to mitigate that.

However, with type-parameters you could make this safer, e.g `Hook a` and `onExercise : AnyChoice -> Update a`, so by instantiating `a` at a type whose visibility you control, you can limit the set of potential implementation sites.

One other nice property of the example in my first post is that I can statically mirror the choices of the underlying template if my `onExercise` doesn’t depend on the particular choice.

So even with interfaces, I think there’s value in having, for example, an `open` keyword on a template that signals that extensions are allowed. This could be implemented via a compiler-synthesized `IsOpen` instance that’s required to define your own `HasExercise` instance.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [November 9, 2021, 5:30pm UTC](https://forum.canton.network/t/implementing-a-wrapper-template/3421/4 "2021-11-09T17:30:28Z")

</div>

I don’t think your suggestion works. `AnyChoice` is not serializable. With interfaces you can write a wrapper for a specific choice but not a generic wrapper.

---

<div class="post-metadata">

**Author:** ![asarpeshkar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/asarpeshkar/32/1601_2.png) [@asarpeshkar](https://forum.canton.network/u/asarpeshkar)\
**Post date:** [November 9, 2021, 5:35pm UTC](https://forum.canton.network/t/implementing-a-wrapper-template/3421/5 "2021-11-09T17:35:11Z")

</div>

In that case, creating an interface for each set of choices I care about would do the trick. For something simple like a hook that wouldn’t be too burdensome, but it would be annoying for more elaborate use-cases.
