Stream: ideas

Topic: Dot notation for multiple field selections


view this post on Zulip Niclas Overby (Apr 20 2024 at 11:29):

Have there been any talks about implementing field selection like this:

selection = record.{ fields1, field2, fields3.{ subfield1, subfield2 } }

Basically the in the same way that it is used for imports.

This could be useful in reducing the noise in field selection, for example in GraphQL clients.

Another option would be with expressions. Something like this:

with = \target, fields -> List.map fields \f -> f target

with record [.field1, .field2]

But that outputs a list instead of a record, and therefore requires encapsulating data in tags.

view this post on Zulip Luke Boswell (Apr 20 2024 at 12:10):

The current syntax for accessing records fields is .field1 which is the same as a function { field1 }_ -> field1 I think. Is that similar?

view this post on Zulip Luke Boswell (Apr 20 2024 at 12:12):

Actually that is in the tutorial https://www.roc-lang.org/tutorial#record-shorthands

view this post on Zulip Luke Boswell (Apr 20 2024 at 12:13):

Nvm me, I misread your idea.

view this post on Zulip Luke Boswell (Apr 20 2024 at 12:15):

I like the syntax of the first one similar to imports.

view this post on Zulip Brendan Hansknecht (Apr 20 2024 at 14:45):

So that is creating a selection record that just copies a bunch of fields from the result record?

The output record is this?

selection = {
    fields1 : ...,
    field2 : ...,
    fields3: {
        subfield1: ...,
        subfield2: ...,
        # dropped any other sub fields
    },
    # dropped any other fields
}

view this post on Zulip Brendan Hansknecht (Apr 20 2024 at 14:49):

Or does it flatten the selected fields?

selection = {
    fields1: ...,
    field2: ...,
    subfields1: ...,
    subfields2: ...,
}

view this post on Zulip Brendan Hansknecht (Apr 20 2024 at 14:53):

Either way, I don't quite understand the goal. You can always pass the original record to a function that takes an open record and uses a subset of the fields.

Ex:

record = # my original gigantic record

subFieldFunc = \{fields1, field2, fields3: { subfield1, subfield2}} ->
    # use all the fields
    # could even directly return
    {fields1, field2, subfield1, subfield2}

subFieldFunc record

view this post on Zulip Niclas Overby (Apr 21 2024 at 06:09):

@Brendan Hansknecht record.{ fields1, field2, fields3.{ subfield1, subfield2 } } is briefer than using a function and opening the record with that.

view this post on Zulip timotree (Apr 21 2024 at 06:30):

You don't even need to define a function. You can use pattern-matching locally like so:

selection =
    { fields1, fields2, fields3: { subfield1, subfield2 } } = record
    { fields1, fields2, fields3: { subfield1, subfield2 } }

Last updated: Jun 16 2026 at 16:19 UTC