# Does DAML has something similar to type overloading?

**URL:** <https://forum.canton.network/t/does-daml-has-something-similar-to-type-overloading/1813>\
**Category:** App Development\
**Created:** [December 16, 2020, 8:13am UTC](https://forum.canton.network/t/does-daml-has-something-similar-to-type-overloading/1813 "2020-12-16T08:13:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![a14843](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@a14843](https://forum.canton.network/u/a14843)\
**Post date:** [December 16, 2020, 8:13am UTC](https://forum.canton.network/t/does-daml-has-something-similar-to-type-overloading/1813/1 "2020-12-16T08:13:26Z")

</div>

Hello, I am new to Haskell / DAML, and have a basic question.

Below is some psuedo code which illustrates what I want to achieve… i.e. I want some kind of overloading which allow a single template to support different product catelogue. Would you please shred me some light on how can I achieve it in DAML?

Many thanks in advance!

```haskell
data ComAProductCode = A1 | A2 | A3 deriving (Eq,Show)
data ComBProductCode = B1 | B2 | B3 deriving (Eq,Show)

data PurchaseOrderForComAorComB a = PurchaseOrderForComAorComB with
    code : a
    quantity : Int
deriving (Eq,Show)

template PurchaseOrderRequest 
    with
        buyer: Party
        seller: Party
        PurchaseOrderForComAorComB : PurchaseOrderForComAorComB a
    where
        signatory buyer
        observer seller
        controller buyer can
            nonconsuming Create: ContractId PurchaseOrderRequest
                with
                    purchaseOrder: PurchaseOrderFromComAorComB
                do

```

---

<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:** [December 16, 2020, 10:35am UTC](https://forum.canton.network/t/does-daml-has-something-similar-to-type-overloading/1813/2 "2020-12-16T10:35:39Z")

</div>

Hi @a14843, welcome to the forum! DAML currently doesn’t support type parameters at the template level so you have a few options:

1. Declare a `ProductCode` variant type that covers both A and B: `data ProductCode = PCComA ComAProductCode | PCComB ComBProductCode`.  
You can then replace your parameter `a` with `ProductCode`. The downside of this approach is that `ProductCode` is a static list. Ie if a company C joins, you can’t easily extend this.
2. Define a template per company: `template PurchaseOrderRequestComA` and `template PurchaseOrderRequestComB`. This has the obvious downside that consumers of `PurchanseOrderRequest` also need to be replicated per company.
3. Type your product code weakly. Ie store it as a `Text`, not an enum.

---

<div class="post-metadata">

**Author:** ![Andrae](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/andrae/32/395_2.png) [@Andrae](https://forum.canton.network/u/Andrae)\
**Post date:** [December 16, 2020, 11:50pm UTC](https://forum.canton.network/t/does-daml-has-something-similar-to-type-overloading/1813/3 "2020-12-16T23:50:41Z")

</div>

It is also worth noting that this restriction only applies to Templates. The rest of your DAML code can use `PurchaseOrderForComAorComB` just fine. It’s only when writing data to the ledger that DAML currently needs to know the precise concrete type for its data.

So generally we use option 1 from @bernhard’s answer above in the template itself, and then either use that sum type for all the code, or define a function to convert it to the generic form in any choice that needs to use it.

---

<div class="post-metadata">

**Author:** ![a14843](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@a14843](https://forum.canton.network/u/a14843)\
**Post date:** [December 17, 2020, 2:36am UTC](https://forum.canton.network/t/does-daml-has-something-similar-to-type-overloading/1813/4 "2020-12-17T02:36:01Z")

</div>

Some more help needed. I tried this:

data ComAProductCode = A1 | A2 | A3  
deriving(Eq, Show)

data ComBProductCode = B1 | B2 | B3  
deriving(Eq, Show)

data AllProductCode = PCComA ComAProductCode | PCComB ComBProductCode  
deriving(Eq, Show)

…

Somewhere inside a template, I refer it:

```
    controller receipent can
        PurchaseOrderRequest_Accept: ContractId Invoice
            do
                now <- getTime
                let 
                    costForInsurance : Optional Decimal = if 
                        purchaseOrder.shippingAgreement == CIF &&
                        purchaseOrder.productCode == A1
                        then Some 3.0 
                        else None

```

The line “purchaseOrder.productCode == A1” gives me an error:

```
• Couldn't match type ‘AllProductCode’ with ‘ComAProductCode’
    arising from a functional dependency between:
      constraint ‘DA.Internal.Record.HasField
                    "productCode" PurchaseOrder ComAProductCode’
        arising from a use of ‘DA.Internal.Record.getField’
      instance ‘DA.Internal.Record.HasField
                  "productCode" PurchaseOrder AllProductCode’
        at <no location info>
• In the first argument of ‘(==)’, namely
    ‘(DA.Internal.Record.getField @"productCode" purchaseOrder)’
  In the second argument of ‘(&&)’, namely
    ‘(DA.Internal.Record.getField @"productCode" purchaseOrder) == A1’
  In the expression:
    (DA.Internal.Record.getField @"shippingAgreement" purchaseOrder)
      == CIF
      && (DA.Internal.Record.getField @"productCode" purchaseOrder) == A1 typecheck
```

---

<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:** [December 17, 2020, 4:12am UTC](https://forum.canton.network/t/does-daml-has-something-similar-to-type-overloading/1813/5 "2020-12-17T04:12:24Z")

</div>

Try changing

```haskell
purchaseOrder.productCode == A1

```

to

```haskell
purchaseOrder.productCode == PCComA A1

```

`A1` is of type `ComAProductCode` so you need to apply the constructor to get a `PCComA`.

---

<div class="post-metadata">

**Author:** ![a14843](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@a14843](https://forum.canton.network/u/a14843)\
**Post date:** [December 17, 2020, 7:48am UTC](https://forum.canton.network/t/does-daml-has-something-similar-to-type-overloading/1813/6 "2020-12-17T07:48:22Z")

</div>

Thanks heap for coming to my recuse in a timely fashion! I can move on with my project now.
