5 ms·
The beauty of the ternary operator is that it isn't one. It's just taking advantage of the fact that if/else is an expression in Kotlin and it turns out that wh
by zmmmmm 7y ago
The beauty of the ternary operator is that it isn't one. It's just taking advantage of the fact that if/else is an expression in Kotlin and it turns out that when if/else yields a value, a dedicated ternary operator is redundant. It's one of a small number of things that I like better about Kotlin than Groovy.
- Someone1234 7y agoThat's actually a really clever (yet simple) design. Have other languages done this before? Now I cannot "unsee" that concept. Why isn't if/else an expression in other languages that just yields a value?! That's a powerful idea.
- cesarb 7y ago> Have other languages done this before? LISP. For a more recent example, Rust.
- int_19h 7y agoIt's very common for languages to adopt an everything-is-an-expression concept, and then make if/else the conditional operator.
- nepeckman 7y agoNim does this as well. Almost any block statement (including creating a new scope with `block`) can be put on the right hand side of an assignment. Whatever value is returned from the block is the result of the expression. All lisp inspired, of course, but with a Pythonic syntax.
- virtualwhys 7y ago> Have other languages done this before? Well, yeah, Scala obviously, the primary language from which Kotlin drew inspiration for its syntax and features.
- Drakar1903 7y agoI wonder why people don't frame Kotlin as the middleman that takes the good parts of both Java and Scala.
- barryhoodlum 7y agoOcaml does this too.
- umanwizard 7y agoRust has this, as well as some other nice ML-inspired[0] features, like structured bindings. That’s one thing I like about Rust: it fuses functional and imperative styles very naturally. [0] “ML” here meaning the family of languages including SML and OCaml, which inspired (to varying degrees) Haskell, Rust, some parts of modern C++, and surely others. Not “machine learning”.