Stream: ideas

Topic: ✔ lambda syntax


view this post on Zulip Richard Feldman (Jul 22 2024 at 22:23):

this might seem kinda random, but it's something I've been thinking about for a long time and finally decided to write it up :big_smile:

what if instead of using backslashes for lambdas, we used a mainstream lambda syntax instead?

today:

Screenshot-2024-07-22-at-6.15.10PM.png

idea:

Screenshot-2024-07-22-at-6.15.48PM.png

view this post on Zulip Richard Feldman (Jul 22 2024 at 22:24):

I had originally considered having this for Roc's lambda syntax (back in 2018 haha), but back then I had this idea for how to do things like parsing and JSON decoding: https://github.com/rtfeldman/elm-json-experiment?tab=readme-ov-file#the-experimental-api

view this post on Zulip Richard Feldman (Jul 22 2024 at 22:25):

and I thought the Ruby/Rust-style lambdas looked weird in that style

view this post on Zulip Richard Feldman (Jul 22 2024 at 22:25):

we never actually ended up using that style, and it was always kind of in the back of my mind to revisit the idea

view this post on Zulip Richard Feldman (Jul 22 2024 at 22:26):

incidentally, I think the most mainstream option, of (foo, bar) -> doesn't work for the reasons discussed in #ideas > tuples for function arguments

view this post on Zulip Richard Feldman (Jul 22 2024 at 22:29):

so ruling that style out (let's not discuss that one here; I don't think it will work out, but if anyone wants to discuss anyway, please start a different #ideas thread about it!) here's why I think Ruby/Rust-style lambdas are worth considering:

view this post on Zulip Richard Feldman (Jul 22 2024 at 22:32):

  1. Both of them look fine to me, but only one of them is used in mainstream languages. To me, this is the main benefit; if we already had Ruby/Rust-style closures, would we use part of our "weirdness budget" to switch to backslash lambdas? I can't really imagine that being the right call.
  2. It's slightly more concise.
  3. I've heard the feedback that backslashes are annoying to type on non-US keyboards, and also cause problems in Vim (I think @Folkert de Vries mentioned that in the context of the old string interpolation syntax maybe?)

view this post on Zulip Richard Feldman (Jul 22 2024 at 22:33):

aside from getting used to it, I don't think changing would take a big effort. We can do the thing we've done several times in the past, where we start accepting the new syntax while still allowing the old syntax (for now), have roc format convert the old syntax to the new syntax, and then eventually deprecate and remove the old syntax

view this post on Zulip Richard Feldman (Jul 22 2024 at 22:34):

anyway, that's the idea! I'm curious what others think :big_smile:

view this post on Zulip Brendan Hansknecht (Jul 22 2024 at 23:11):

Personally, I wouldn't consider rust's syntax more mainstream. Rust is still a rather niche language in many of the contexts where we get users from.

view this post on Zulip Brendan Hansknecht (Jul 22 2024 at 23:11):

I think the \... -> syntax makes sense to people who are used to js arrow functions. And that is a much more mainstream language.

view this post on Zulip Brendan Hansknecht (Jul 22 2024 at 23:13):

That said, I think many more users trip up due to our function calling syntax rather than our function definition syntax. So I would argue that function definition syntax is not hard to grok in roc. Thus, not very strange.

view this post on Zulip Brendan Hansknecht (Jul 22 2024 at 23:14):

Also, without { ... }, I find parsing the function body much harder with |...|. It feels more standalone and doesn't give any indicator to pay attention to indentation.

view this post on Zulip Brendan Hansknecht (Jul 22 2024 at 23:15):

Cause it look too similar to (...) which does not care about indentation in the same way

view this post on Zulip Brendan Hansknecht (Jul 22 2024 at 23:16):

eg these two look too similar but mean very different things:

credits = List.map songs |song|
    "Performed by $(song.artist)"

credits = List.map songs (song)
    "Performed by $(song.artist)"

view this post on Zulip Isaac Van Doren (Jul 23 2024 at 01:01):

This syntax looks weird to me. I encountered lambdas first in JS and then C# which both use arrows (as does Java). It was quite some time before I ever saw the pipe syntax. I suspect a lot of other people will be more familiar with the arrow syntax.

view this post on Zulip Isaac Van Doren (Jul 23 2024 at 01:04):

I think the arrow works well with the trailing lambda syntax also because it looks makes it look like there is something unfinished that needs to continue on the next line. An open curly serves a similar purpose so without the arrow or curly it looks odd at first to me.

view this post on Zulip Luke Boswell (Jul 23 2024 at 01:37):

I also really like the current syntax. :smiley:

view this post on Zulip Musab Nazir (Jul 23 2024 at 01:48):

Basically just echoing other's thoughts. I'd never seen this Ruby style lambdas before. JS and C#'s were the ones I first learned and they are much closer to roc's current syntax

view this post on Zulip Folkert de Vries (Jul 23 2024 at 07:31):

I've heard the feedback that backslashes are annoying to type on non-US keyboards, and also cause problems in Vim

On my keyboard these are the same key, which for the ruby/rust syntax I have to push twice, and the issue in vim was that \" was escaped and hence doing vim motions like si" for select everything enclosed by "" did not work. That is not a problem with our lambda syntax

view this post on Zulip Richard Feldman (Jul 23 2024 at 10:41):

ok cool! Well this one seems like a straightforward conclusion: let's keep it as-is! :big_smile:

view this post on Zulip Notification Bot (Jul 23 2024 at 10:41):

Richard Feldman has marked this topic as resolved.

view this post on Zulip Maksim Volkau (Nov 07 2024 at 11:09):

My 5 cents, that from the parser standpoint, the current Roc approach uniquely identifies the lambda by the first \ character. Which is great for the machine as well, and may allow experimenting with the new strange ideas e.g. a closure shortcut.

view this post on Zulip Georges Boris (Dec 27 2024 at 13:09):

Folks - just additional feedback here.

While creating some nushell scripts today - nushell uses the same lambda syntax as rust - I noticed a few pain points:

Just my 2c! In general I like the approach as:

My other negative feeling is how args used to look closer to type definitions and I thought using -> across function typedefs, lambdas and pattern matching made the general syntax more cohesive.

view this post on Zulip Anthony Bullard (Dec 27 2024 at 13:28):

Georges Boris said:

My other negative feeling is how args used to look closer to type definitions and I thought using -> across function typedefs, lambdas and pattern matching made the general syntax more cohesive.

One of the concerns some had in the move to Purity Inference is that though the lambda was always defined with -> an effectful function was annotated using =>. This move to Rust/Ruby lambda syntax actually stops people from making that strong connection and makes the annotation syntax more distinct

view this post on Zulip Yunti (Dec 30 2024 at 16:56):

Was the |something| syntax settled on rather than the more mainstream (somethingb) -> because of issues working with the ml style function application syntax? Presumably it would work ok with the Algol/C style - as those languages make it work without issue, or is there something else in the language that blocks it. The current proposal | …| does look weird. Didn’t realise that Rust had it (shows that I’ve probably forgotten most things in Rust as i never got round to using it in practice)

view this post on Zulip Richard Feldman (Dec 30 2024 at 16:58):

we talked about (something) -> but it causes a lot of problems/confusion with tuples

view this post on Zulip Richard Feldman (Dec 30 2024 at 16:59):

also the -> is slightly strange in the context of Roc functions specifically, because at the type level there's an important distinction between -> and =>

view this post on Zulip Richard Feldman (Dec 30 2024 at 16:59):

|something| doesn't use an arrow, so it doesn't suggest anything one way or the other about the lambda's purity

view this post on Zulip Yunti (Dec 30 2024 at 17:04):

Ah yes I can see how having two arrows for pure and effectful would be different to other C syntax style language so would add problems. Sorry just seen that you had resolved the thread to keep it as is with the \ -> style. (Presumably that will become \ -> or \=> depending on the type of function?

view this post on Zulip Richard Feldman (Dec 30 2024 at 17:04):

we later talked about it more in a different thread and decided to go with |something|

view this post on Zulip Richard Feldman (Dec 30 2024 at 17:05):

we talked about having an arrow and making it so you had to write either -> or => depending on whether the function was pure or effectful, but it seemed like more of a hassle than useful

view this post on Zulip Yunti (Dec 30 2024 at 17:08):

:grinning: Ah sorry for creating noise…. Still digging around the various threads… yup |something| makes sense rather than -> or => although that the arrows feel more consistent and clear… but these things are always trade offs. Thx

view this post on Zulip Luke Boswell (Dec 30 2024 at 21:22):

So is there an accepted future lambda syntax now?

view this post on Zulip Luke Boswell (Dec 30 2024 at 21:23):

Just read this comment from Anton which I guess answers my question

https://roc.zulipchat.com/#narrow/channel/231634-beginners/topic/A.20couple.20of.20remaining.20questions/near/491283640

view this post on Zulip Luke Boswell (Dec 30 2024 at 21:24):

Are we chaning the type annotation at all though?

view this post on Zulip Sam Mohr (Dec 30 2024 at 21:26):

Nope, we don't seem to like how it looks: #ideas > Switch function types to use vertical bars @ 💬

view this post on Zulip Anton (Dec 31 2024 at 10:31):

Just read this comment from Anton which I guess answers my question

I thought it was confirmed, not 100% sure though

view this post on Zulip Sam Mohr (Dec 31 2024 at 15:30):

I think we all licked our fingers and stuck them in the air to feel for the wind of disagreement, and felt stillness, and we called that confirmation. I've not seen someone say, "let's vote to confirm this" before except maybe once or twice.

view this post on Zulip Anton (Dec 31 2024 at 15:34):

So poetic :)

view this post on Zulip Anthony Bullard (Dec 31 2024 at 15:47):

Funny that it was marked as resolved then

view this post on Zulip Anthony Bullard (Dec 31 2024 at 15:48):

How about we unresolve, make a poll, and then resolve after a day or two?

view this post on Zulip Anthony Bullard (Dec 31 2024 at 15:51):

Reading back through this thread I see two conflicting things about this

view this post on Zulip Richard Feldman (Dec 31 2024 at 15:51):

I think it's fine, worth trying out!

view this post on Zulip Anthony Bullard (Dec 31 2024 at 15:51):

Richard Feldman said:

ok cool! Well this one seems like a straightforward conclusion: let's keep it as-is! :big_smile:

There's this on July 23

view this post on Zulip Anthony Bullard (Dec 31 2024 at 15:51):

Richard Feldman said:

we later talked about it more in a different thread and decided to go with |something|

And this yesterday (after it was marked resolved)

view this post on Zulip Richard Feldman (Dec 31 2024 at 15:52):

yeah the latter is more recent

view this post on Zulip Richard Feldman (Dec 31 2024 at 15:52):

the first conversation was before purity inference

view this post on Zulip Richard Feldman (Dec 31 2024 at 15:52):

and parens-and-commas

view this post on Zulip Anthony Bullard (Dec 31 2024 at 15:52):

Cool, so I'll finish this off then with

WE ARE GOING TO TRY |arg1, arg2| body syntax

view this post on Zulip Norbert Hajagos (Dec 31 2024 at 15:52):

The poll will look like: "Does our bdfn think we should do it?":big_smile:

view this post on Zulip Anthony Bullard (Dec 31 2024 at 15:53):

BDFN is a lovely term

view this post on Zulip Richard Feldman (Dec 31 2024 at 15:56):

https://www.roc-lang.org/bdfn for those who aren't familiar :big_smile:

view this post on Zulip Anthony Bullard (Jan 04 2025 at 03:15):

Did anyone make a issue for this? If not, I will in the morning (and assign it myself)

view this post on Zulip Sam Mohr (Jan 04 2025 at 03:18):

I didn't do it, I'm not seeing one, so it doesn't look to exist

view this post on Zulip Sam Mohr (Jan 04 2025 at 03:18):

Please consider adding that to the pinned Recent Syntax Changes issue

view this post on Zulip Sam Mohr (Jan 04 2025 at 03:19):

If you don't have perms, I can do it


Last updated: Jun 16 2026 at 16:19 UTC