Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support for something like Rust if-let #22285

tknawara started this conversation in General Discussion
Discussion options

Hi team,

I'm very new here, so apologies if I didn't provide enough details. I'm wondering if you have given any thoughts around supporting something like rust if let

The basic idea is to allow for destructuring in if condition and bind everything in the if scope, you can already do that with match of course, but I often find myself only interested in a single case of an enum for example while the remaining cases won't need special handling for them.

This is already supported in different scala constructs like for comprehensions and would be nice to do something like

if Some(v) = myOption then
 ...

instead of

if myOption.isDefined then
 val Some(v) = myOption
 ....

It gets complicated if you want to use v further in the condition though

if Some(v) = myOption && v.xyz then
 ...
You must be logged in to vote

Replies: 5 comments 1 reply

Comment options

This reminds me dart-lang/language#4205

You must be logged in to vote
0 replies
Comment options

Similar discussion happened in F# fsharp/fslang-suggestions#705

You must be logged in to vote
0 replies
Comment options

Maybe use the function .collect?

You must be logged in to vote
0 replies
Comment options

What's the benefit over:

myOption match
 case Some(v) if v.xyz =>
 ...
 case None =>

?
More compact?

You must be logged in to vote
1 reply
Comment options

Comment options

There's been a (pre-)pre-SIP on this topic with a lengthy explanations of the motivations for such a feature: https://contributors.scala-lang.org/t/pre-sip-s-improving-the-expressiveness-of-conditional-control-flow/6488

There are plans to work on a proper SIP in the near future. Any help is welcome obviously.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /