# Custom bounded types

**URL:** <https://forum.canton.network/t/custom-bounded-types/2477>\
**Category:** App Development\
**Tags:** daml\
**Created:** [April 19, 2021, 9:41am UTC](https://forum.canton.network/t/custom-bounded-types/2477 "2021-04-19T09:41:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Kalcza](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/tamas_kalcza/32/1788_2.png) [@Tamas\_Kalcza](https://forum.canton.network/u/Tamas_Kalcza)\
**Post date:** [April 19, 2021, 9:41am UTC](https://forum.canton.network/t/custom-bounded-types/2477/1 "2021-04-19T09:41:38Z")

</div>

Is it possible to create custom, bounded types in Daml? For example:

```haskell
template Example
  with
    party : Party
    ageLimit : Bounded 18 65
  where
    signatory party

```

where `ageLimit` argument would be limited to `Int`s between 18 and 65.

I have found the `Bounded` typeclass, but not sure how to use it and if that is applicable to this use case.

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [April 19, 2021, 9:49am UTC](https://forum.canton.network/t/custom-bounded-types/2477/2 "2021-04-19T09:49:32Z")

</div>

```haskell
module Main where

import Daml.Script

newtype MyBounded = MyBounded Int deriving (Show, Enum)

instance Bounded MyBounded where
  minBound = MyBounded 18
  maxBound = MyBounded 65

setup : Script ()
setup = script do
  debug $ ([minBound..maxBound] : [MyBounded])
  return ()

```

prints

```haskell
[MyBounded 18,MyBounded 19,MyBounded 20,MyBounded 21,MyBounded 22,MyBounded 23,MyBounded 24,MyBounded 25,MyBounded 26,MyBounded 27,MyBounded 28,MyBounded 29,MyBounded 30,MyBounded 31,MyBounded 32,MyBounded 33,MyBounded 34,MyBounded 35,MyBounded 36,MyBounded 37,MyBounded 38,MyBounded 39,MyBounded 40,MyBounded 41,MyBounded 42,MyBounded 43,MyBounded 44,MyBounded 45,MyBounded 46,MyBounded 47,MyBounded 48,MyBounded 49,MyBounded 50,MyBounded 51,MyBounded 52,MyBounded 53,MyBounded 54,MyBounded 55,MyBounded 56,MyBounded 57,MyBounded 58,MyBounded 59,MyBounded 60,MyBounded 61,MyBounded 62,MyBounded 63,MyBounded 64,MyBounded 65]

```

---

<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:** [April 19, 2021, 9:51am UTC](https://forum.canton.network/t/custom-bounded-types/2477/3 "2021-04-19T09:51:58Z")

</div>

Note that this does not enforce any bounds. `MyBounded 15` will compile and run just fine. Enforcing those invariants can only be done indirectly via `ensure` clauses at the moment. See [Should runtime/API respect namespace scopes?](https://discuss.daml.com/t/should-runtime-api-respect-namespace-scopes/2114) and [Extending Ensure for non Templates - #4 by Gary\_Verhaegen](https://discuss.daml.com/t/extending-ensure-for-non-templates/2472/5) for some discussion.

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [April 19, 2021, 9:52am UTC](https://forum.canton.network/t/custom-bounded-types/2477/4 "2021-04-19T09:52:27Z")

</div>

Though note that `debug (MyBounded 1)` also works, so this may not be what you want here.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [April 19, 2021, 9:59am UTC](https://forum.canton.network/t/custom-bounded-types/2477/5 "2021-04-19T09:59:41Z")

</div>

Paraphrasing what everyone has said above, I think `Bounded` just provides you with functions that return the `min` and `max` of a type, which is useful for syntactic sugar like `[Jan .. Jul]`. As the other contributors said, it doesn’t actually enforce these bounds at the type level.

---

<div class="post-metadata">

**Author:** ![Tamas\_Kalcza](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/tamas_kalcza/32/1788_2.png) [@Tamas\_Kalcza](https://forum.canton.network/u/Tamas_Kalcza)\
**Post date:** [April 19, 2021, 10:01am UTC](https://forum.canton.network/t/custom-bounded-types/2477/6 "2021-04-19T10:01:11Z")

</div>

Actually I think my example was wrong in the first place. So what I’m trying to do is something like this:

```haskell
template Example
  with
    party : Party
    a : Limited Int
    b : Limited RelTime
  where
    signatory party

```

And when such a contract would be create, the party would able to specify the actual limits, like:

```haskell
Example with party, a = limited 18 65, b = limited (hours 2) (days 30)

```

where `limited` could be a smart constructor.

I deliberately avoided to use `Bounded` to indicate I’m okay to create something completely different if needed.

Well, I guess I can create my own data type for that. So I think the lesson here is that 1) there is nothing built-in for that, 2) I cannot have compile-time safety due the nature of the problem.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [April 19, 2021, 10:07am UTC](https://forum.canton.network/t/custom-bounded-types/2477/7 "2021-04-19T10:07:22Z")

</div>

Would a simple `data Range a = Range with inf : a, sup: a` work? What do you need the smart constructor for?

---

<div class="post-metadata">

**Author:** ![Tamas\_Kalcza](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/tamas_kalcza/32/1788_2.png) [@Tamas\_Kalcza](https://forum.canton.network/u/Tamas_Kalcza)\
**Post date:** [April 19, 2021, 10:09am UTC](https://forum.canton.network/t/custom-bounded-types/2477/8 "2021-04-19T10:09:45Z")

</div>

That’s actually what I have. Although I think I’ll change the name, but that’s irrelevant.

I wanted a smart constructor to prevent clients to use the data constructor and to keep the internals hidden. It’s all in flux though…

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [April 19, 2021, 10:14am UTC](https://forum.canton.network/t/custom-bounded-types/2477/9 "2021-04-19T10:14:24Z")

</div>

I think this is where you go wrong:

> [@Tamas\_Kalcza](#):
>
> where `limited` could be a **_smart constructor_**.

As the discussions @cocreature pointed to explain, there is no such thing as a smart constructor in Daml. There is also no way to hide a constructor at the LF level.

This is not about compile-time safety, this is about push-to-the-ledger-time safety.

If you want trusted validations, you have to have a trusted party validating the data. I think this could be a good use-case for triggers: you create your contract with the trigger party as an observer, and the (trusted) trigger runs the validations and signs your contract.

---

<div class="post-metadata">

**Author:** ![Tamas\_Kalcza](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/tamas_kalcza/32/1788_2.png) [@Tamas\_Kalcza](https://forum.canton.network/u/Tamas_Kalcza)\
**Post date:** [April 19, 2021, 7:57pm UTC](https://forum.canton.network/t/custom-bounded-types/2477/10 "2021-04-19T19:57:09Z")

</div>

Just for the sake of argument how is this achieved in the standard library? For example `RelTime` cannot be constructed using the data constructor, just via certain functions. Does `-- | HIDE` have special meaning?

```haskell
newtype RelTime = -- | HIDE
  RelTime with microseconds : Int

```

---

<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:** [April 19, 2021, 8:21pm UTC](https://forum.canton.network/t/custom-bounded-types/2477/11 "2021-04-19T20:21:40Z")

</div>

`-- | HIDE` only hides it from the documentation, it has no semantic effect.

`daml-stdlib` doesn’t solve this problem. If you have a `RelTime` field on a template you can send whatever you want over the ledger API and no validation will take place. That’s what the thread I linked above discusses [Should runtime/API respect namespace scopes?](https://discuss.daml.com/t/should-runtime-api-respect-namespace-scopes/2114).

We do try to make it hard to accidentally construct it manually so directly in Daml you’ll have a hard time (I expect you can bypass the checks if you try hard enough but it’s not trivial):

1. The actual type definition is in `DA.Time.Types`, `DA.Time` only reexports the type but not the constructor.
2. The compiler has a hardcoded list of internal modules that you cannot import without getting an error.
3. `DA.Time.Types` has special treatment in `data-dependencies` which means you also cannot directly bypass it that way.
