4 ms·
It’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.
by tomtheelder 2y ago
It’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.
- runevault 2y agoYou misunderstood. They were talking specifically about languages that only have ternary operators as a way to do if-as-expression, and why they prefer languages with either real if-else if-else expressions or full switch/pattern matching as expression.