Switch Expression (on conditions) #3350
Replies: 9 comments 1 reply
-
Beta Was this translation helpful? Give feedback.
-
I don't think that's quite it, as I'm assuming Tony is looking for non-constant patterns as well? Relational operators are coming in 9. |
Beta Was this translation helpful? Give feedback.
-
You can use var Value = true switch {
_ when Value1 == Value2 => "Hello",
_ when Value2 > Value3 => "World",
_ when Value4 > Value5 && Value6.Foo() => "Complex",
_ => "Default",
}; Although |
Beta Was this translation helpful? Give feedback.
-
@Sunnie-Shine Wow! That's sweet! I'm glad that exists. It would be really nice if I could just emit the |
Beta Was this translation helpful? Give feedback.
-
And what about the ternary operator? The syntax is a bit different but even less verbose :) ...
var Value =
Value1 == Value2 ? "Hello"
: Value2 > Value3 ? "World"
: Value4 > Value5 && Value6.Foo() ? "Complex"
: "Default"
; |
Beta Was this translation helpful? Give feedback.
-
The current Pattern Matching switch expression is unreadable, un-c-like syntax. And it brings 'and' 'or' keywords that will obscure with '&&' '||' symbols. The tranditional c/cpp/c# are syntax-strict, and use () {} || && to make codes readable and maintainable. Too free and oral syntax will make language unmaintainable, the pattern matching switch expression is a dangerous start. Why not change the switch code to c-like syntax: int age = 10;
var stage = switch(age)
{
(age < 0) => LiftStage.Prenatal,
(age < 2) => LifeStage.Infant,
(age < 4) => LifeStage.Toddler,
_ => LifeStage.LateAdult,
}; or int longvariant = 10;
var stage = switch(longvariant as t)
{
(t < 0) => LiftStage.Prenatal,
(t < 2) => LifeStage.Infant,
(t < 4) => LifeStage.Toddler,
_ => LifeStage.LateAdult,
}; or use other keyword "map" to avoid syntax collision? int t = 10;
var stage = map(t)
{
(t < 0) => LiftStage.Prenatal,
(t < 2) => LifeStage.Infant,
(t < 4) => LifeStage.Toddler,
_ => LifeStage.LateAdult,
}; |
Beta Was this translation helpful? Give feedback.
-
The And for the Specifically:
|
Beta Was this translation helpful? Give feedback.
-
I maybe know the reason, but this syntax is so ..... oral and unreadable and make csharp much more complex and chaos, It's maybe not a good start. |
Beta Was this translation helpful? Give feedback.
-
I find myself using ternarys like this var Value =
Value1 == Value2 ? "Hello"
: Value2 > Value3 ? "World"
: Value4 > Value5 && Value6.Foo() ? "Complex"
: "Default"; And saw the alternative var Value = true switch {
_ when Value1 == Value2 => "Hello",
_ when Value2 > Value3 => "World",
_ when Value4 > Value5 && Value6.Foo() => "Complex",
_ => "Default",
}; That said, it would be nice to have a "cleaner" version of the switch expression. Something like var Value = switch {
Value1 == Value2 => "Hello",
Value2 > Value3 => "World",
Value4 > Value5 && Value6.Foo() => "Complex",
_ => "Default",
}; But I don't know that it comes up often enough to spend time adding it to the language. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It would be great if I could do the following:
Beta Was this translation helpful? Give feedback.
All reactions