# Record as a Key

**URL:** <https://forum.canton.network/t/record-as-a-key/1065>\
**Category:** App Development\
**Tags:** daml\
**Created:** [August 19, 2020, 8:09am UTC](https://forum.canton.network/t/record-as-a-key/1065 "2020-08-19T08:09:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean\_Safar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/jean_safar/32/186_2.png) [@Jean\_Safar](https://forum.canton.network/u/Jean_Safar)\
**Post date:** [August 19, 2020, 8:09am UTC](https://forum.canton.network/t/record-as-a-key/1065/1 "2020-08-19T08:09:40Z")

</div>

Hi,

Sorry for the basic question, but I can’t find it in the doc … if I have a data type that i need to be in a Set , is there (like for a template) a way to indicate what attributes compose a Key?

```haskell
data MyElem = MyElem with 
   foo: Text
   bar: Text
   something: Int
  somethingelse: Party
deriving (Eq, Show)

```

How can I have `Set MyElem`

---

<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:** [August 19, 2020, 9:50am UTC](https://forum.canton.network/t/record-as-a-key/1065/2 "2020-08-19T09:50:32Z")

</div>

Sets (`DA.Next.Set`) are implemented as Maps. `Set MyElem` is just `Map MyElem ()`. DAML currently only has native maps with text keys (type `TextMap`. So the `Map` and `Set` implementations in `DA.Next.Set` and `DA.Next.Map` require one to write serialization and deserialization code to `Text`.

You can use `show` as serialization, but then have to write parsing code, which is non-trivial.

This is clearly awkward so we have “Generic Maps” and thus also Generic Sets on our roadmap. You can already test Generic Maps as follows:

Set your project to build DAML-LF 1.dev (which will eventually become 1.9) by adding this to your `daml.yaml`:

```haskell
build-options:
  - --target=1.dev

```

Then `import DA.Map` in your project, add `Ord` to the list of typeclasses derived by `MyElem` and treat `Map MyElem ()` as your set. The `DA.Set` wrapper library doesn’t exist yet, but it would be an easy adaption from [daml/Set.daml at a080b47e347277e099167fc59affde9dac313342 · digital-asset/daml · GitHub](https://github.com/digital-asset/daml/blob/a080b47e347277e099167fc59affde9dac313342/compiler/damlc/daml-stdlib-src/DA/Next/Set.daml).

---

<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:** [August 19, 2020, 1:53pm UTC](https://forum.canton.network/t/record-as-a-key/1065/3 "2020-08-19T13:53:43Z")

</div>

I feel the need to point out that any dar produced using `--target=1.dev` is very much **not supported** for any kind of production use. Even just uploading it to a persistent ledger is likely to break things. This is really just meant for dev; please only upload such dars to throwaway ledgers.

While there is a point at which we will create a DAML LF `1.9`, it would certainly not be safe to assume `1.9` will be exactly what `1.dev` is today, nor a strict superset of it.

---

<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:** [August 19, 2020, 2:55pm UTC](https://forum.canton.network/t/record-as-a-key/1065/4 "2020-08-19T14:55:44Z")

</div>

> [@bernhard](#):
>
> Then `import DA.Map` in your project, add `Ord` to the list of typeclasses derived by `MyElem` and treat `Map MyElem ()` as your set.

So, are you saying that the ordering determines the keys used for hashing, in answer to OP? I expect I’ve misunderstood this, but if not, isn’t this a pretty hefty constraint? Is it not enough to have some sort of a `Hashable` typeclass?

Would this mean that in OP’s example, you’d need to write an `instance Ord` that then picks which particular field to order on - i.e. using `deriving Ord` in this particular case wouldn’t work?

Btw. is `Ord` a total order?

---

<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:** [August 19, 2020, 3:18pm UTC](https://forum.canton.network/t/record-as-a-key/1065/5 "2020-08-19T15:18:12Z")

</div>

I’m not in the loop on the specific work for the new `DA.Map`, but I think I can offer some elements of an answer nonetheless.

First off, yes, the `Ord` typeclass is intended to represent a total order. `deriving Ord`, when it works, does that.

Regarding the requirement for `Ord`, while one could argue it is not, strictly speaking, a requirement to store something in an associative data structure, and a `Hashable` typeclass could do the trick if the underlying implementation were indeed a hash table, it’s also possible to implement maps (and sets) using binary trees based on ordering the given elements, as is the case for [Data.Set](http://hackage.haskell.org/package/containers-0.6.3.1/docs/Data-Set.html) in Haskell. Based on @bernhard’s stated requirement to add `Ord`, I suspect the current prototype for `DA.Map` is based on similar techniques.

---

<div class="post-metadata">

**Author:** ![Leonid\_Rozenberg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_rozenberg/32/48_2.png) [@Leonid\_Rozenberg](https://forum.canton.network/u/Leonid_Rozenberg)\
**Post date:** [August 20, 2020, 2:42pm UTC](https://forum.canton.network/t/record-as-a-key/1065/6 "2020-08-20T14:42:27Z")

</div>

@Jean_Safar

Something like this is what you’re looking for

```haskell
import DA.Text
-- We need a separator character that is not in either of our text fields
-- nor a digit
-- nor a Party (https://github.com/digital-asset/daml/blob/ba74146c10d149687d7f42972a9ced6203c3f807/daml-lf/spec/daml-lf-1.rst#literals)
sep : Text
sep = ";"

instance M.MapKey MyElem where

  keyToText c = c.foo
      <> sep <> c.bar
      <> sep <> M.keyToText c.something
      <> sep <> M.keyToText c.somethingelse

  keyFromText t = case splitOn sep t of
                    [foo, bar, somethingT, somethingelseT] ->
                      MyElem with
                        foo
                        bar
                        something = M.keyFromText somethingT
                        somethingelse = M.keyFromText somethingelseT
                    _ -> error $ "Malformed key" <> t

```

---

<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:** [August 20, 2020, 5:01pm UTC](https://forum.canton.network/t/record-as-a-key/1065/7 "2020-08-20T17:01:13Z")

</div>

Note that this "`;` can never be used in a string ever" is likely to bite you in the future. If adding an `Ord` instance does not work for you, and you really need to support translation to and from a text format, I’d recommend implementing a simple string-based replacement à la htmlencode: on the way in, replace `;` with `&semicolon` and `&` with `&amp`, and do the opposite on the way out. Then you _know_ there is no `;` in the encoded strings and you can safely use that as a delimiter.

---

<div class="post-metadata">

**Author:** ![Leonid\_Rozenberg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_rozenberg/32/48_2.png) [@Leonid\_Rozenberg](https://forum.canton.network/u/Leonid_Rozenberg)\
**Post date:** [August 20, 2020, 6:05pm UTC](https://forum.canton.network/t/record-as-a-key/1065/8 "2020-08-20T18:05:03Z")

</div>

+1 to Gary’s suggestion!

---

<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:** [August 24, 2020, 2:14pm UTC](https://forum.canton.network/t/record-as-a-key/1065/9 "2020-08-24T14:14:59Z")

</div>

Let me add some clarification around the upcoming generic map type:

Together with the generic Map type, DAML-LF has gained a builtin generic comparison function. This works for roughly anything that is serializable (not quite accurate but a decent approximation). For incomparable values, e.g., functions, this throws a runtime error.

For DAML, we want to prevent invalid values statically. Currently, this is accomplished by requiring an `Ord` instance. However, the `Ord` instance will not actually be used by the map. Instead it will use DAML-LF’s builtin notion of equality. Since nothing stops you from defining an `Ord` instance for a record containing a function, you can technically still produce something that crashes.

Having two notions of ordering is rather confusing so there is an open question here if it would be better to forbid user-defined `Ord` instances to make sure that `Ord` instances always match up with the builtin notion of equality which is also much faster.
