Thought experiment: Namespaced record names

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.

1 Like

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. :stuck_out_tongue:

1 Like

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, and showed alternatives.

4 Likes

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.

2 Likes

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

2 Likes

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

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

Full example: https://ellie-app.com/6SwPX9x69a1/0

7 Likes

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 :heart:

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

1 Like

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

1 Like

Record destructuring is documented in the official documentation, 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:

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.

3 Likes

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

I just noticed that this specific use is documented on faq.elm-community.org, which links to more details in the blog post Intro to Records in Elm.

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