# Thought experiment: Namespaced record names

**URL:** <https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957>\
**Category:** Request Feedback\
**Created:** [March 21, 2018, 1:27pm UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957 "2018-03-21T13:27:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![robin.heggelund](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/robin.heggelund/32/1330_2.png) [@robin.heggelund](https://discourse.elm-lang.org/u/robin.heggelund)\
**Post date:** [March 21, 2018, 1:27pm UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/1 "2018-03-21T13:27:07Z")

</div>

Thinking out loud on how to make it easier to have flat records. Would this be a good fit for Elm?

Let’s not talk about nested record updates, as I know that’s been done at length in the past.

> **[Thought Experiment: Namespaced record fields](https://dev.to/skinney/thought-experiment-namespaced-record-fields-4l80)**
>
> An idea for making flat records more workable

---

<div class="post-metadata">

**Author:** ![pdamoc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pdamoc/32/36_2.png) [@pdamoc](https://discourse.elm-lang.org/u/pdamoc)\
**Post date:** [March 21, 2018, 1:52pm UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/2 "2018-03-21T13:52:18Z")

</div>

I would vote for an improved nested record update.

`person.pet` is unambiguous to me. It refers to the value in the field `pet` of `person`.

`{ person.pet | name = "Fifo"}` should by the same token refer to an updated version of the record in `person.pet`.

If this becomes supported, `{ person | pet = { person.pet | name = "Fido" } }` becomes valid and I don’t think it is all that hard to understand. I would take this over the proposed alternative any day of the week.

I do concede that `:P` has its charm. 😛

---

<div class="post-metadata">

**Author:** ![rtfeldman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rtfeldman/32/50_2.png) [@rtfeldman](https://discourse.elm-lang.org/u/rtfeldman)\
**Post date:** [March 21, 2018, 3:07pm UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/3 "2018-03-21T15:07:23Z")

</div>

There was a discussion some time ago about this, but I don’t remember where! (The old elm-discuss mailing list maybe?) The conclusion was that introducing nested record update syntax would be a step in the wrong direction for the language.

The tl;dr was “In practice, code that wants nested record updates always seems to improve if it gets refactored such that it no longer wants nested record updates. We shouldn’t add a language feature whose appeal is that it makes it easier to write suboptimal code.”

I covered some of the downsides of code that wants nested record updates in [Scaling Elm Apps](https://www.youtube.com/watch?v=DoA4Txr4GUs), and showed alternatives.

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [March 21, 2018, 3:17pm UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/4 "2018-03-21T15:17:15Z")

</div>

I read your proposal, but I found it hard to follow. So my instinct is that it seems to be introducing special syntax when Elm is very minimal with syntax in a good way, and that it makes records feels complex.

---

<div class="post-metadata">

**Author:** ![robin.heggelund](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/robin.heggelund/32/1330_2.png) [@robin.heggelund](https://discourse.elm-lang.org/u/robin.heggelund)\
**Post date:** [March 21, 2018, 3:32pm UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/5 "2018-03-21T15:32:13Z")

</div>

If I may ask, what was it that made it hard to follow? Nice to know for future blog posts.

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [March 22, 2018, 8:43am UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/7 "2018-03-22T08:43:26Z")

</div>

I like this valid syntax using records destructuring and `as` that was just posted on Slack (thank you Francesco):

```elm
renamePersonPet : String -> Person -> Person
renamePersonPet newName ({ pet } as person) =
    { person | pet = { pet | name = newName } }

```

Full example: [https://ellie-app.com/6SwPX9x69a1/0](https://ellie-app.com/6SwPX9x69a1/0)

---

<div class="post-metadata">

**Author:** ![opvasger](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@opvasger](https://discourse.elm-lang.org/u/opvasger)\
**Post date:** [March 22, 2018, 9:20am UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/8 "2018-03-22T09:20:03Z")

</div>

I have never seen this before. Have i missed the documentation for this syntax? it’s much nicer than what I’ve been doing so far! with `let/in` Thank you ❤

EDIT: just realized this was the the first thing mentioned in the “Lesser Known Syntax”-thread, which I participated in… 😛

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [March 22, 2018, 9:45am UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/9 "2018-03-22T09:45:27Z")

</div>

> [@robin.heggelund](#):
>
> If I may ask, what was it that made it hard to follow? Nice to know for future blog posts.

It wasn’t your writing, it was just the code. Too much special syntax compared with plain records which are a familiar concept.

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [March 22, 2018, 9:53am UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/10 "2018-03-22T09:53:45Z")

</div>

Record destructuring is documented in the [official documentation](http://elm-lang.org/docs/records#pattern-matching), but I don’t think that the `as` keyword is (except for module import), this would be a useful addition.

Also this particular use of both may not be intuitive either (I used them for pattern matching but did not think about it for nested records).

What is also nice is that it works for several sub records:

```elm
renamePersonPets name1 name2 ({pet1, pet2} as person) =
  { person
    | pet1 = { pet1 | name = name1 }
    , pet2 = { pet2 | name = name2 }
  }

```

and is limited to one level deep, so this does not encourage deep nesting.

---

<div class="post-metadata">

**Author:** ![opvasger](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@opvasger](https://discourse.elm-lang.org/u/opvasger)\
**Post date:** [March 22, 2018, 12:18pm UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/11 "2018-03-22T12:18:50Z")

</div>

Yes, it was the `as` keyword I was commenting on; I’m very familiar with record-destructuring outside of that case 🙂

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [March 23, 2018, 8:59pm UTC](https://discourse.elm-lang.org/t/thought-experiment-namespaced-record-names/957/12 "2018-03-23T20:59:40Z")

</div>

I just noticed that this specific use is documented on [faq.elm-community.org](http://faq.elm-community.org/#how-can-i-pattern-match-a-record-and-its-values-at-the-same-time), which links to more details in the blog post [Intro to Records in Elm](https://medium.com/elm-shorts/intro-to-records-in-elm-51bc5e933a57).

This FAQ is nice by the way, thank you to the contributors, also I appreciate very much the overall elm-community effort.
