-
Couldn't load subscription status.
- Fork 1.1k
Support for something like Rust if-let #22285
-
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 ...
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
Replies: 5 comments 1 reply
-
This reminds me dart-lang/language#4205
Beta Was this translation helpful? Give feedback.
All reactions
-
Similar discussion happened in F# fsharp/fslang-suggestions#705
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Maybe use the function .collect?
Beta Was this translation helpful? Give feedback.
All reactions
-
What's the benefit over:
myOption match case Some(v) if v.xyz => ... case None =>
?
More compact?
Beta Was this translation helpful? Give feedback.
All reactions
-
https://google.github.io/comprehensive-rust/pattern-matching/let-control-flow/if-let.html
Maybe save some typing ?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1