# Is there a way to limit the length of a Listy within the type definition?

**URL:** <https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900>\
**Category:** App Development\
**Tags:** daml\
**Created:** [January 6, 2021, 1:06pm UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900 "2021-01-06T13:06:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darko](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/darko/32/42_2.png) [@Darko](https://forum.canton.network/u/Darko)\
**Post date:** [January 6, 2021, 1:06pm UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900/1 "2021-01-06T13:06:55Z")

</div>

Assume a contract template definition that looks as follows:

```haskell
template foo
  with
    item0: Decimal
    item1: Decimal
    item2: Decimal
    item3: Decimal
    item4: Decimal
  where
    ....

```

I want to refactor this template to use a list of decimals.

```haskell
template foo
  with
    items: [Decimal]
  where
    ....

```

Is there a way to limit the length of `items` to 5 on a type-level or should one use `ensure` on the template which would be checked only during runtime?

---

<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:** [January 6, 2021, 1:16pm UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900/2 "2021-01-06T13:16:07Z")

</div>

Your best option here is `ensure`. There are some techniques to enforce this type of stuff at the type-level that work in Haskell but in DAML you run into various issues in particular around `data-dependencies` and even in Haskell the complexity rarely pays off.

---

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [January 6, 2021, 3:27pm UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900/3 "2021-01-06T15:27:30Z")

</div>

Is there a reason a tuple wouldn’t work here? Assuming that the list length needs to be exactly 5.

```haskell
type MyItems = (Decimal, Decimal, Decimal, Decimal, Decimal)

```

Or a record type?

```haskell
data MyItems = MyItems
  with
    item1 : Decimal
    item2 : Decimal
    ...
  deriving (Eq, Show)

```

---

<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:** [January 6, 2021, 3:32pm UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900/4 "2021-01-06T15:32:45Z")

</div>

Record types and tuples both force an exact number of elements not a limit on the number of elements.

Apart from that, they also make it super hard to do anything generically. E.g., for a list incrementing each element by one is a simple `map (+1)`. For a tuple, you have to define that yourself and you have to define it separately for each tuple length.

---

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [January 6, 2021, 3:36pm UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900/5 "2021-01-06T15:36:21Z")

</div>

> [@cocreature](#):
>
> Apart from that, they also make it super hard to do anything generically. E.g., for a list incrementing each element by one is a simple `map (+1)` . For a tuple, you have to define that yourself and you have to define it separately for each tuple length.

Good point

> [@cocreature](#):
>
> Record types and tuples both force an exact number of elements not a limit on the number of elements.

From the example it seems as if the length of the list will always be 5 as the items were previously parameters on a template (which I think is a record type anyway)

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [January 7, 2021, 2:31am UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900/6 "2021-01-07T02:31:57Z")

</div>

> [@anthony](#):
>
> From the example it seems as if the length of the list will always be 5 as the items were previously parameters on a template (which I think is a record type anyway)

Assuming this, I would go further and put

```haskell
data List5 a = List5 { a0: a, a1: a, a2: a, a3: a, a4: a }
  deriving (Eq, Show, Ord, Functor, Foldable, Traversable)

```

which gives you a good number of “list” operations such as @cocreature mentions _without_ having to define them yourself, such as `fmap (+1)` and `for`.

I can’t speak to the complications with data-dependencies that @cocreature mentions, though.

---

<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:** [January 7, 2021, 7:55am UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900/7 "2021-01-07T07:55:59Z")

</div>

@stephen’s definition is perfectly fine wtr to `data-dependencies` and is indeed a good option if you want a fixed length rather than a limit on the length (which is how I read the question initially). It still has the downside that you have to define separate types for each length of list which can get a bit messy.

---

<div class="post-metadata">

**Author:** ![Darko](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/darko/32/42_2.png) [@Darko](https://forum.canton.network/u/Darko)\
**Post date:** [January 7, 2021, 4:05pm UTC](https://forum.canton.network/t/is-there-a-way-to-limit-the-length-of-a-listy-within-the-type-definition/1900/8 "2021-01-07T16:05:25Z")

</div>

The use case I was looking at definitely intended to limit the size, not have a fixed size list. Still very good inputs. thank you very much all!
