Suggestions?

Hey,

is there a possibility to suggest new features/suggestions?

Greetings

Finki

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

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()

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

2 Likes

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() }
2 Likes

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

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) }