3 ms·
As an aside for those wondering what the name for this is to see what other languages support it, this is a case 'if' being an expression rather than a statemen
by monocasa 4y ago
As an aside for those wondering what the name for this is to see what other languages support it, this is a case 'if' being an expression rather than a statement which allows it to be composed within other expressions.
- smitty1e 4y agoA predicate?
- tialaramex 4y agoNo, if, and match, and most things are expressions, and thus have a (typed) value. In a typical programming language we can write something like x = 5; assigning 5 to the variable x. That 5 there is an expression, we can write x = (3 * 2) - 1; with the same effect, that's a more complicated expression. We can call a function here: x = f(3); or if our language has methods we can even call those: x = some.function(parameters); In Rust I can write x = if it.should_be_nine() { 9 } else { 5 }; // because if is an expression Since Rust's loops are also expressions, x = loop { let stuff = get_stuff(); if stuff.is_good() { break stuff } }; // ... once the stuff is good, break out of the loop and we'll assign that stuff to x
- smitty1e 4y agoUnderstood, these things are expressions, but must they nit evaluate to a boolean in order for if/switch/match to branch correctly? My informal understanding of a predicate may need refining.
- tialaramex 4y agoI think the thing you're missing is that in some languages we're allowed to use these constructs as expressions. Not that they take expressions which is common in modern non-toy languages, but that they themselves are expressions. It might be you've just always used languages where you can do this, or that you assumed you can but actually you never tried in a language you use. In C for example we can't write this: int x = if (1 == 2) { 3 } else { 4 }; // Not valid C syntax C provides a special operator, the only one in the language which has three operands and thus it's often just called "the ternary operator" although other ternary operators can technically exist, to achieve the same thing: int x = (1 == 2) ? 3 : 4; // Valid C syntax using ternary operator But, how about this? int x = switch 3 { case 1: 4; case 2: 8; default: 0; }; // Can't do this either There's no way to express that in C or C++ today. The closest is the "Immediately Invoked Lambda" from C++ which is illustrated nearby in this thread. Barry Revzin apparently proposed a way to do this in C++ 26 because C++ wants to have pattern matching and pattern matching kinda sucks without this feature.
- andix 4y agoExactly. Here is an overview of switch expressions for c#, for many cases you can replace if/else if/else with those. The pattern matching is very powerful too: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
- usrusr 4y ago"If/else expressions", as opposed to if/else merely doing control flow. Really surprising that they took so long to become mainstream, yet still more than a decade after I started using them I still occasionally miss the more "expression-like" look of the ternary in languages that don't offer both.