The return
keyword (PR) is now available for early returns in Roc! You can use it in if
or when
, even nested ones.
onlyCalculateWhenPositive = \n ->
firstVal =
if n <= 0 then
return NoResult
else
n + 5
Found (firstVal * 2)
The try
keyword (PR) has also been merged. It should be in nightlies by . You can use it in place of ?
, which is still supported for now but may be deprecated soon.
contents = try File.read! "my-file.txt"
contents = "my-file.txt" |> try File.read!
contents = File.read! "my-file.txt" |> try
yooooo, awesome work @Sam Mohr!!! :heart_eyes:
this is going to make Purity Inference so ergonomic! :grinning_face_with_smiling_eyes:
Really liking this direction of not having too many symbols, but using verbs instead :clap:
Roc is becoming a Forth dialect
Too many parens and too few periods to be a Forth dialect
Wouldn't the example clean up to look like the following?
onlyCalculateWhenPositive = \n ->
if n <= 0 then
return NoResult
firstVal = n + 5
Found (firstVal * 2)
(it seems strange to use return
alongside else
, and to have the return
"dodge around" the assignment that it's contained within)
Kevin Gillette said:
Wouldn't the example clean up to look like the following?
onlyCalculateWhenPositive = \n -> if n <= 0 then return NoResult firstVal = n + 5 Found (firstVal * 2)
This is a feature we want to support but haven't implemented yet: https://github.com/roc-lang/roc/issues/7105
Between PR 1 and PR 2, the try
and ?
operators are now no longer just desugaring to other code, but are using a proper type-checking syntax! You should get better compiler errors when you use try
or ?
in a weird way. For example:
invalidTry = \{} ->
nonResult = "abc"
x = nonResult?
Ok (x * 2)
invalidTry {}
Gives this error:
── INVALID TRY TARGET in /code/proj/Main.roc ───────────────────────────────────
This expression cannot be tried with the `?` operator:
6│ x = nonResult?
^^^^^^^^^^
I expected a Result, but it actually has type:
Str
Hint: Did you forget to wrap the value with an `Ok` or an `Err` tag?
This is such a relief! Thanks @Sam Mohr!
should get better compiler errors when you use
try
or?
in a weird way
Challenge accepted :salute:
Anthony Bullard said:
This is such a relief! Thanks Sam Mohr!
Happy to help! Let me know if you run into any issues.
My next task is to improve handling of early returns/try/? in statements, that pairs with this change well
Is there a preference to use try
or ?
? Will both stay in Roc or will one be deprecated in the future?
I think try
may go away if/when static dispatch(with parens-and-commas) lands
But I dont think it has to. Zig has try
before the expression
Either would work, but ? is syntactically much more compatible with static dispatch, so it's pretty likely we'll get rid of try
to maintain the "one way to do things" goal
I hope we keep both through the first few months of static dispatch and get feedback before committing to one
Okay, we can do that
It wouldn't be too much of a compiler complexity burden to do so
Do we want to be more symbol-y or more keyword-y I think is the big design question
I tried to create a language in the same design space that had NO keywords and had commas and parens
But it had almost zero sugar
And no prefix, postfix, or binary operators :rofl:
Jamie Neubert Pedersen said:
Really liking this direction of not having too many symbols, but using verbs instead :clap:
But I think this sentiment is common, especially for people coming from C/ALGOL descendants
A lot of work has been moving towards symbols where possible, but there are some places where operators are just better, since they're great at visually breaking up things
The main example is when arrows. We discussed maybe doing when
and then
:
when result is
Ok val then abc
Err err then def
The branch patterns and bodies are harder to distinguish than with arrows
when result is
Ok val -> abc
Err err -> def
Syntax highlighting helps, but it's still worse IMO
Maybe it is a good thing to have ?
and try
for the moment. I don't have any preference. I realise, that I am using both without a system. Sometimes ?
and sometimes try
. Maybe this is a good experiment, if a symbol or a keyword is better. But I hope that in a 1.0, where will be a clear system and only one way to do it.
I agree with you on everything there
Ooh ?!
is back on the table, then :smiley:
Oops I mean !?
And now !?.
between names
well with parens and commas it would always be foo!()?
foo!?
would be a type mismatch because the !
suffix naming convention would only be for functions, and functions aren't Result
s, so using ?
on one directly would always be a type mismatch :big_smile:
But Task
is gone, so I can no longer alias Task.fromResult
to really
Ohhh gotcha, I didn't realize empty ()
would be required. (Makes sense.)
So then
the |> old |> try idiomatic! |> roc! style
the.new().idiomatic!()?.roc!(style)
so if I get this right try ( anythingThatIsAResult )
is equivalent to ( anythingThatIsAResult )?
correct?
and where is this new aTask!()?
style coming from?
Yes, those are equivalent
The new style is from the pretty much accepted static dispatch proposal:
https://docs.google.com/document/d/1OUd0f4PQjH8jb6i1vEJ5DOnfpVBJbGTjnCakpXAYeT8/edit
Do they both interrupt the function and early return on Err? Visually (or thinking of other languages), when used in a .chain the ?
looks to me like it would just interrupt the chain.
They literally use the same code, so yes
Sam Mohr said:
The new style is from the pretty much accepted static dispatch proposal:
https://docs.google.com/document/d/1OUd0f4PQjH8jb6i1vEJ5DOnfpVBJbGTjnCakpXAYeT8/edit
How do proposals become "accepted"? Is that process documented somewhere?
Lmao nope
I had kinda thought Richard just did the BDFL thing
That's true, but it's more that Rich is just the one who made the proposal this time
In general:
Nothing formal
We do have an RFC repo, but it's not really used
Cool, so no 5 Stage process, quarterly plenaries, and consensus gathering amongst 100s of stakeholders?
:rofl:
This is vibes-based proposals
(This was a TC-39 subtweet)
Both processes make sense for where the respective languages are, and their environment
We have this https://www.roc-lang.org/community#ideas
Last updated: Jul 26 2025 at 12:14 UTC