Suggestions?


#1

Hey,

is there a possibility to suggest new features/suggestions?

Greetings

Finki


#2

Yes, you’re welcome to post your suggestions here.


#3

okay the best way to show is to show it:

instead of:

if (condition){
   doSomething()
   return true
}
return false

more like this:

return if (condition)
    doSomething()

#4

I think that anyone reading this code would expect that this method would return the return value of doSomething, and not true.


#5

why not:

return condition.also { if (it) doSomething() }

This is actually valid Kotlin and more concise (inlining will make this work as expected). If you really want to you could add a function:

inline fun Boolean.whenTrue(action:() ->Unit) {
    if(this) action()
}

return condition.whenTrue { doSomething() }

#6

Thanks that is a nice way thanks for your attention :wink:


#7

you can also use more generalized version:

inline fun <T> T.alsoWhen(t: T, fn: (T) -> Unit): T {
    if (this == t) { fn(this) }
    return this
}

// usage
return condition.alsoWhen(true) { println(it) }
return number.alsoWhen(42) { println(it) }