Stream: ideas

Topic: Augmented assignment operators for `var`


view this post on Zulip Agus Zubiaga (Sep 20 2024 at 14:05):

I wonder if we should have a "pipe and assign" operator, so instead of:

files_ = files_ |> Dict.insert path sha

you can do something like:

files_ |= Dict.insert path sha

view this post on Zulip Agus Zubiaga (Sep 20 2024 at 14:07):

and probably += et al.

view this post on Zulip Sam Mohr (Sep 20 2024 at 14:17):

I totally agree that *-assign operators would be nice. I think we should have a separate discussion on those? Wouldn't want to block this feature on those

view this post on Zulip Notification Bot (Sep 20 2024 at 14:20):

3 messages were moved here from #ideas > for and var by Agus Zubiaga.

view this post on Zulip Agus Zubiaga (Sep 20 2024 at 14:25):

I really like that |= Dict.insert ... is almost as concise as the usual style in imperative languages where Dict.insert would mutate the dict itself, but without the characteristics we don't like about that

view this post on Zulip Agus Zubiaga (Sep 20 2024 at 14:26):

(the reassignment happens at the call site, so it's easy to spot)

view this post on Zulip Sam Mohr (Sep 20 2024 at 15:13):

Yes, among the operators that this would introduce, the |= would probably be far be the best addition

view this post on Zulip Sam Mohr (Sep 20 2024 at 16:18):

It seems like a pipe assign plus math assigns are a good start:

view this post on Zulip Sam Mohr (Sep 20 2024 at 16:22):

Definitely think we shouldn't go for ++ and -- operators, they lead to confusion about when the incrementation happens

view this post on Zulip Agus Zubiaga (Sep 20 2024 at 17:12):

I think an assignment wouldn’t be an expression in Roc, so it wouldn’t work anyway, but I agree we don’t need them

view this post on Zulip Sky Rose (Sep 21 2024 at 00:14):

I'm not sure about %= and ^=. You get a different sort of number out. Like, length *= 2 still gives you a length, but length ^= 2 would give an area, you'd more likely write area = length ^ 2. They're rare enough that I think it'd be fine and not frustrating to have to write them out with =. If we skip them, that's 2 fewer weird operators.

view this post on Zulip Brendan Hansknecht (Sep 24 2024 at 05:37):

Is ^ power? I assumed it was bitwise xor

view this post on Zulip Brendan Hansknecht (Sep 24 2024 at 05:38):

I think %= can be quite useful in general for numbers limited to a specific modulo for mathematic purposes

view this post on Zulip Brendan Hansknecht (Sep 24 2024 at 05:38):

I personally am not sure I like |= compared to being explicit.

view this post on Zulip Brendan Hansknecht (Sep 24 2024 at 05:39):

But it probably is reasonable

view this post on Zulip Kilian Vounckx (Sep 24 2024 at 09:16):

Brendan Hansknecht said:

Is ^ power? I assumed it was bitwise xor

In some languages (haskell is one I can think of right now) it is power. Also in mathematical formulas written in ascii, power is often denoted with it (see latex for example). In programming however I have seen ^ mostly for bitwise xor as well. I think ** is used more often for power, like in python (and for some reason also in haskell)

view this post on Zulip Kevin Gillette (Sep 25 2024 at 19:30):

Instead of a new operator, what about just inferring that the left-hand-side is also the first pipeline component when not specified, i.e. the following would be equivalent:

a = a |> f |> g

a = |> f |> g

view this post on Zulip Kevin Gillette (Sep 25 2024 at 19:30):

maybe it'd be formatted as =|> but it doesn't need to actually be a combined operator


Last updated: Jun 16 2026 at 16:19 UTC