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
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
and I thought the Ruby/Rust-style lambdas looked weird in that style
we never actually ended up using that style, and it was always kind of in the back of my mind to revisit the idea
incidentally, I think the most mainstream option, of (foo, bar) -> doesn't work for the reasons discussed in #ideas > tuples for function arguments
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:
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
anyway, that's the idea! I'm curious what others think :big_smile:
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.
I think the \... -> syntax makes sense to people who are used to js arrow functions. And that is a much more mainstream language.
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.
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.
Cause it look too similar to (...) which does not care about indentation in the same way
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)"
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.
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.
I also really like the current syntax. :smiley:
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
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
ok cool! Well this one seems like a straightforward conclusion: let's keep it as-is! :big_smile:
Richard Feldman has marked this topic as resolved.
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.
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:
| at least on my keyboard, sit on the exact key as \ but require an additional modifier (this is minor - safe to ignore)| is a regex meaningful character which makes searching for it more troublesome. I usually forget to escape it, get the wrong result and change my query. (this can be an actual concern?)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.
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
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)
we talked about (something) -> but it causes a lot of problems/confusion with tuples
also the -> is slightly strange in the context of Roc functions specifically, because at the type level there's an important distinction between -> and =>
|something| doesn't use an arrow, so it doesn't suggest anything one way or the other about the lambda's purity
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?
we later talked about it more in a different thread and decided to go with |something|
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
: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
So is there an accepted future lambda syntax now?
Just read this comment from Anton which I guess answers my question
Are we chaning the type annotation at all though?
Nope, we don't seem to like how it looks:
Just read this comment from Anton which I guess answers my question
I thought it was confirmed, not 100% sure though
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.
So poetic :)
Funny that it was marked as resolved then
How about we unresolve, make a poll, and then resolve after a day or two?
Reading back through this thread I see two conflicting things about this
I think it's fine, worth trying out!
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
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)
yeah the latter is more recent
the first conversation was before purity inference
and parens-and-commas
Cool, so I'll finish this off then with
WE ARE GOING TO TRY |arg1, arg2| body syntax
The poll will look like: "Does our bdfn think we should do it?":big_smile:
BDFN is a lovely term
https://www.roc-lang.org/bdfn for those who aren't familiar :big_smile:
Did anyone make a issue for this? If not, I will in the morning (and assign it myself)
I didn't do it, I'm not seeing one, so it doesn't look to exist
Please consider adding that to the pinned Recent Syntax Changes issue
If you don't have perms, I can do it
Last updated: Jun 16 2026 at 16:19 UTC