Hi all, Please take a look at <this new feature pr...
# language-proposals
m
Hi all, Please take a look at this new feature proposal. How it looks to you?
d
Is such a feature worth implementing to shorten the
if (x) return y
pattern by only two characters?
h
I am not against it, but
? :
syntax (ternary operator) is not used in Kotlin before, and is unlikely to be introduced: https://kotlinlang.org/docs/control-flow.html#if-expression
👍 1
f
Using
?
in the context of bool is a bad idea for the consistency of the language, as it always is about
null
in every other context.
👍 1
And for
null
you don't need it:
a ?: return b
m
If ‘a’ (from the ticket example) is any expression in Kotlin I guess nullable values are already handled there, is it? And for
c ?: return d
it’s not the same use-case we return c if it’s not null but in
return? a : b
we return b if a expression is true
h
Just from reading this syntax:
if (a) return b
read as: if a is true, return b. From left to right, reading follows code execution, first condition, then the optional action and its value.
return? a : b
read as: return b if a is true. To understand the effect, you must read the optional action first, then the condition, and then the value used by the action. Reading does not follow the code execution. With complex expression, this is harder to understand, especially for a newcomer.
Copy code
if (oldUser == null || newUser == null || repo == null) return null

return? oldUser == null || newUser == null || repo == null : null
m
I agree it’s harder to read. If this is something worth implementing we can modify the design to ‘look’ more intuitive when you read it
Just one question, I am new here. Is there some analytics for new features to see how often they are used? If there is, can we remove the feature if it’s not used? Is that something that’s done in practice?
h
Removing features, especially removing a language syntax is a very big breaking change and should be avoided. Yes, sometimes a new major release introduces new errors, but mostly bugs. To add a new feature its design is important. https://kotlinlang.org/docs/kotlin-evolution.html#principles-of-pragmatic-evolution, it is not hard to read :)
🙏 1
j
Copy code
fun <T : Any?> runIf(b: Boolean, runIt: () -> T): T? {
   return if (b) {
      runIt.invoke()
   } else {
      null   
   }
}
Copy code
fun main() {
   val a = runIf(true) {
      "testing 1"
   }
   println(a) // prints testing 1

   val b = runIf(false) {
      "testing 1"
   }
   println(b) // prints null

}
I'm assuming the use-case is something like if authenticated, then execute this, otherwise don't?