Stream: beginners

Topic: Is roc going to support a "spread" operator?


view this post on Zulip Oliver Schöning (Oct 21 2021 at 08:51):

I am just curious what your opinions are on the nice modern JS synax sugar for objects and arrays:
[ ...ListA, ...ListB ]
{ ...props, ...moreProps }

view this post on Zulip Richard Feldman (Oct 21 2021 at 11:22):

personally I honestly hadn't given it much thought...what do others think?

view this post on Zulip Folkert de Vries (Oct 21 2021 at 11:29):

I wonder how often this would be useful and better than List.append or a record update

view this post on Zulip Oliver Schöning (Oct 21 2021 at 11:44):

It happens frequently enough in Elm that I do miss this particular feature from JS. Because otherwise I have to put a confusing parentesis before and after my last argument list in a view)
In React I have completely stopped using any append function since this feature.

This feature might make more sense in JS since the JS approach for a "new" object would be {...oldObject, newProp = { ...stuff } }
(Which I would prefer as record update incidentally as a way to onboard js folks hehe)

But my personal opinions on language design hardly matter in the grand scheme of things :laughing:

view this post on Zulip Folkert de Vries (Oct 21 2021 at 11:47):

for completeness, do you have an example from elm where this spreading would be useful?

view this post on Zulip Oliver Schöning (Oct 21 2021 at 12:19):

I'm booted into my work OS right now. In an hour or so I will copy paste some code I am actually using and not making up at the top of my head!

view this post on Zulip Wolfgang Schuster (Oct 21 2021 at 14:43):

For arrays/lists I'd rather use as then I can continue piping it, or do the concat in the middle after modifying listA

listA
    |> List.concat listB

view this post on Zulip Wolfgang Schuster (Oct 21 2021 at 14:45):

For the other, its syntactic sugar for merging records it seems? What happens when both records have the same key but different values? Do you blindly take the second record? If records get really large this could be nice, but for small records it seems like it might hide the details too much :shrug:

view this post on Zulip Folkert de Vries (Oct 21 2021 at 14:51):

So I think the use case here is a scenario like

Html.div (a ++ b) [ Html.text "foobar" ]

Which because roc does not have a ++ infix operator would actually be

Html.div (a |> List.append b) [ Html.text "foobar" ]

Versus

Html.div [..a, ..b] [ Html.text "foobar" ]

But, I wonder how often this really comes up, and whether you would not rather write

attributes = 
    a |> List.append b

Html.div attributes [ Html.text "foobar" ]

If the construction of attributes is non-trivial

view this post on Zulip Oliver Schöning (Oct 21 2021 at 15:02):

Maybe you folks are right. I had a look at my code and it wasn't that bad. I think I can just keep piping in case that I have multiple Lists or do a concat. Not worth polluting the language with more syntax. Keep it as small as possible imo


Last updated: Jul 06 2025 at 12:14 UTC