Hullaballoonatic
10/31/2019, 6:21 PMelse ""
, else 0
, etc?:
return myStr + if (myCondition) myPostfix else ""
return myVal + if (myCondition) myAddition else 0
obviously the nicest syntax would probably simple exclude the else
, but that of course does not compilekarelpeeters
10/31/2019, 6:33 PMif(c) x else ""
too!David Silva
10/31/2019, 6:33 PMreturn myStr + myCondition ? myPostfix : ""
Anyway, It might help if you move the suffix computation to its own function 🤷♂️
return myStr + computeSuffix()
..
private fun computeSuffix() = if (myCondition) mySuffix else ""
Hullaballoonatic
10/31/2019, 6:34 PMfun String.appendIf(cond: Boolean, postFix: String)
but that's even more wordy in use and less readableAlowaniak
10/31/2019, 6:34 PMmyStr + (myPostfix.takeIf { myCondition } ?: "")
work?Shawn A
10/31/2019, 6:34 PMfun <T> Boolean.whenTrue(value: T, otherwise: T): T {
return if(this) value else otherwise
}
😆Hullaballoonatic
10/31/2019, 6:39 PMAlowaniak
10/31/2019, 6:41 PMHullaballoonatic
10/31/2019, 6:42 PMLuke
10/31/2019, 6:52 PMfun String.emptyIf(condition: Boolean) = if (condition) "" else this
? So you would have return myStr + myPostfix.emptyIf(condition)
Hullaballoonatic
10/31/2019, 6:54 PMfun String.given(condition) = if (condition) this else ""
fun String.unless(condition) = if (condition) "" else this
fun String.unless(condition: (String) -> Boolean) = if (condition(this)) "" else this
is nice tooLuke
10/31/2019, 7:01 PMorEmpty()
karelpeeters
10/31/2019, 7:15 PMbuildString {
if (cond) append(str)
}
Hullaballoonatic
10/31/2019, 7:31 PM