6 ms·
instead of "expression" you meant "exception", right?
by TurningCanadian 2y ago
instead of "expression" you meant "exception", right?
- whalesalad 2y agoexpression as-in s-expression (ex: lisp)
- Jtsummers 2y agoIn the statement/expression-oriented axis of languages, Go is a statement oriented language (like C, Pascal, Ada, lots of others). This is in contrast to expression oriented languages like the Lisp family, most, if not all, functional languages, Ruby, Smalltalk and some others. Expressions produce a value, statements do not. That's the key distinction. In C, if statements do not produce a value. In Lisp, if expressions do. This changes where the expression/statement is able to be used and, consequently, how you might construct programs.
- runevault 2y agoA simple example for anyone who might not appreciate why this can be so nice. In languages where if is a statement (aka returns no value), you'd write code like int value; if(condition) { value = 5; } else { value = 10; } Instead of just int value = if(condition) {5} else {10} Some languages leave ifs as statements but add trinary as a way to get the same effect which is an acceptable workaround, but at least for me there are times I appreciate an if statement because it stands out more making it obvious what I'm doing.
- ahci8e 2y agoHow would this work if I need to update multiple variables? int value1 = 0; int value2 = 0; if (condition) { value1 = 8; value2 = 16; } else { value1 = 128; value2 = 256; } Would I have to repeat the if expression twice? int value1 = if (condition) { 8 } else { 128 }; int value2 = if (condition) { 16 } else { 256 };
- tomtheelder 2y agoDepends on the language a bit, but a common feature in these languages is the tuple. Using a tuple you would end up with something like: let (value1, value2) = if (condition) { (8, 16) } else { (16, 256) } Or else you’d just use some other sort of compound value like a struct or something. Tuple is just convenient for doing it on the fly.
- runevault 2y agohah we gave basically the same example on the same minute. I love destructuring so much, I don't know if I'd want to use a language without it anymore.
- tomtheelder 2y agoIt’s actually so painful to go back to languages without destructuring and pattern matching.
- runevault 2y agoAs someone who writes a fair bit of c# making switch and if's into expressions and adding Discriminated Unions (which they are actually working on) are my biggest "please give me this." Plus side I dabble in f# which is so much more expressive.
- throwaway9502 2y ago[dead]
- lyu07282 2y agoSame for me in the Scala vs. Java world, it's hard once you get used to how awesome expressions over statements and algebraic data types/case enums/"discriminated unions" are. But I haven't done much C# (yet) myself, could you clarify for me: does C# have discriminated unions? I didn't think the language supported that (only F# has them)?
- tomtheelder 2y agoIt’s only an acceptable workaround in the case of two conditions, but you’re still out of luck if you have >2 branches and no match expression.
- runevault 2y agoYou can technically do some craziness with nested ternary operators but they look awful and if you write them you will regret it later.
- tomtheelder 2y agoTrue, but I would have to categorize that as an unacceptable workaround haha
- koito17 2y agoNot necessarily. In many Lisps you can bind the result of a condition like so. (let [thing (cond pred-1 form-1 ... pred-n form-n)] (do something with thing)) This makes laying out React components in ClojureScript feel "natural" compared to JSX/TSX, where instead one nests ternaries or performs a handful of early returns. Both of these options negatively impact readability of code.
- runevault 2y agocond isn't a trinary operator it is more of a switch statement. Trinary is expressly let x = condition ? truevalue : falsevalue You can do shenanigans by having something along the lines of let x = condition1 ? truevalue1 : (condition2 ? truevalue2 : falsevalue)
- koito17 2y agocond is neither an operator nor a statement, it's an expression. This is a demonstration of a conditional expression handling multiple conditions, which GP wanted. More importantly, pattern matching is not necessary here.