# Guarding against errant field accessors/ record selectors

**URL:** <https://forum.canton.network/t/guarding-against-errant-field-accessors-record-selectors/3653>\
**Category:** App Development\
**Created:** [January 4, 2022, 8:34pm UTC](https://forum.canton.network/t/guarding-against-errant-field-accessors-record-selectors/3653 "2022-01-04T20:34:59Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [January 4, 2022, 8:34pm UTC](https://forum.canton.network/t/guarding-against-errant-field-accessors-record-selectors/3653/1 "2022-01-04T20:34:59Z")

</div>

Consider

```haskell
data W 
  = WithoutField
  | WithField with
      field : Int

getMyField : W -> Int
getMyField a = a.field

t : Script ()
t = do

  let w = WithField 10

  debug $ getMyField w

  let wo = WithoutField
  debug $ getMyField wo

```

The code above compiles, but the script will fail with an error.

1. AFAIK, there’s no way to turn this into an error, and I can’t find a warning. Is that correct?
2. Is there any way to statically check for usage of missing record selectors? I know that I (😄) would never write such code, so asking for a friend.

---

<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 4, 2022, 8:48pm UTC](https://forum.canton.network/t/guarding-against-errant-field-accessors-record-selectors/3653/2 "2022-01-04T20:48:38Z")

</div>

You are correct that there is no warning for this unfortunately and we don’t have any separate static checks for this either. Your best option is to not define those types in the first place and instead split your records and variants:

```haskell
data W = WithoutField | WithField WithField

data WithField = WithFieldR with field : Int

```

Then you cannot use the field on the wrong constructor without getting a type error. Of course that just brings back the question of how you can enforce this …

---

<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:** [January 4, 2022, 8:57pm UTC](https://forum.canton.network/t/guarding-against-errant-field-accessors-record-selectors/3653/3 "2022-01-04T20:57:48Z")

</div>

Thank you.
