# Disambiguate accessing to contract fields

**URL:** <https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211>\
**Category:** App Development\
**Created:** [April 23, 2020, 8:25am UTC](https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211 "2020-04-23T08:25:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [April 23, 2020, 8:25am UTC](https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211/1 "2020-04-23T08:25:57Z")

</div>

I have a couple of templates that share a field name (in this case `name`)

```haskell
template DivulgeableAsset
  with
    owner: Party
    name: Text
  where
    signatory owner

    -- choices...

template PrivateAsset
  with
    owner: Party
    name: Text
  where
    signatory owner

    -- choices...

```

Later, in DAML Script, I would like to retrieve contracts that instantiate these templates and extract their respective `name`s. I do something like the following:

```haskell
run : Script ()
run = do
  owner <- fromSome $ partyFromText "owner"
  divulgee <- fromSome $ partyFromText "divulgee"

  -- expect the owner to see both the divulgeable...
  divulgeableAssets <- query @DivulgeableAsset owner
  assertEq ["first"] (map ((name : DivulgeableAsset -> Text) . snd) divulgeableAssets)

  -- ... and the private asset
  privateAssets <- query @PrivateAsset owner
  assertEq ["second"] (map ((name : PrivateAsset -> Text) . snd) privateAssets)

```

Note that I explicitly hinted the compiler the type of the `name` function. If I don’t, I get the following error:

```haskell
Ambiguous occurrence ‘name’
It could refer to
   either the field ‘name’,
          imported from ‘Main’ at /some/path/daml/Test.daml:13:1-11
          (and originally defined
             at /some/path/daml/Main.daml:47:5-8)
       or the field ‘name’,
          imported from ‘Main’ at /some/path/daml/Test.daml:13:1-11
          (and originally defined
             at /some/path/daml/Main.daml:34:5-8)

```

Is there a cleaner way to disambiguate between those two functions (or, in general, to extract those fields)?

The full project is available [here](https://github.com/stefanobaghino-da/test-5541-data-migration).

---

<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 23, 2020, 8:29am UTC](https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211/2 "2020-04-23T08:29:39Z")

</div>

You can use record-dot-syntax which will resolve the ambiguity automatically. So for your example, that would be something like the following

```haskell
  assertEq ["first"] (map (\(_, asset) -> asset.name) divulgeableAssetsSeenByOwner)

```

---

<div class="post-metadata">

**Author:** ![Shaul](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/shaul/32/117_2.png) [@Shaul](https://forum.canton.network/u/Shaul)\
**Post date:** [April 24, 2020, 8:50pm UTC](https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211/3 "2020-04-24T20:50:05Z")

</div>

Is this pattern that dlint could potentially catch? @cocreature @shaynefletcher

---

<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 25, 2020, 4:41am UTC](https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211/4 "2020-04-25T04:41:37Z")

</div>

In general no, HLint does not have enough information to judge whether something is a record field or not. You could probably make something that works as long as the record and the usage of the field are in the same module but I expect the implementation effort to be fairly high

---

<div class="post-metadata">

**Author:** ![shaynefletcher](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/shaynefletcher/32/567_2.png) [@shaynefletcher](https://forum.canton.network/u/shaynefletcher)\
**Post date:** [April 25, 2020, 7:46pm UTC](https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211/5 "2020-04-25T19:46:05Z")

</div>

I concur. It’s easy enough to locate constructions like `name : DivulgeableAsset -> Text` however the syntax alone does not carry enough “context” for DLint to classify it as a field selector (as @cocreature says, which would then induce a lot of “special case” implementation effort) so if it were to issue hints, they would be highly speculative and often likely false positives which would be annoying and consequently quickly get disabled. Actually, the sample @stefanobaghino-da provides is somewhat interesting in that he reached for selectors in the first instance over dot-syntax which I’d think might be considered as slightly unusual when programming DAML and probably a consequence of prior Haskell experience. In fact if DAML already had the long desired and highly anticipated `NoFieldSelectors` feature it wouldn’t have been an option 😃

---

<div class="post-metadata">

**Author:** ![shaynefletcher](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/shaynefletcher/32/567_2.png) [@shaynefletcher](https://forum.canton.network/u/shaynefletcher)\
**Post date:** [April 25, 2020, 7:51pm UTC](https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211/6 "2020-04-25T19:51:50Z")

</div>

I’d have to write a program to test this but I expect sections to work here too e.g. `map ((.name) . snd) divulgeableAssetsSeenByOwner` and wonder if inference succeeds if phrased that way? I guess here is another way to dodge the ambiguity : `map (\(_, DivulgeableAsset {name}) -> name) divulgeableAssetsSeenByOwner`

---

<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 25, 2020, 8:08pm UTC](https://forum.canton.network/t/disambiguate-accessing-to-contract-fields/211/7 "2020-04-25T20:08:11Z")

</div>

Using a section, i.e., `(.name) . snd` does work but at least to me that is significantly harder to read and understand than `\(_, asset) -> asset.name` so I would not recommend that option here. Of course there might be other examples where the section is useful.
