Giorgio Antonioli
04/09/2019, 1:44 PMtrue. Is there a shorter way (in the stdlib maybe) to write the following?
someBooleanCondition.takeIf { true }?.let { "dummy-string" }
E.g. (in pseudo-code)
someBooleanCondition.ifTrue { "dummy-string" }diesieben07
04/09/2019, 1:46 PMif (someCondition) "dummy-string" else nulldiesieben07
04/09/2019, 1:47 PM"dummy-string".takeIf { someCondition }Giorgio Antonioli
04/09/2019, 1:47 PMwbertan
04/09/2019, 1:47 PMinline fun <T> Boolean?.ifTrue(asas: (Boolean) -> T): T? = this?.takeIf { it == true }?.let(asas)wbertan
04/09/2019, 1:48 PMval b = true
val r: String? = b.ifTrue { "asas" }Giorgio Antonioli
04/09/2019, 1:49 PMifTrue extension if it's not already there in the stdlibdiesieben07
04/09/2019, 1:49 PMtakeIf is that extension, you just have to swap condition and the value you want.thanksforallthefish
04/09/2019, 1:50 PMgildor
04/09/2019, 2:08 PMghedeon
04/09/2019, 2:12 PMtakeIf has its own usage in chains, no need to overthink in simple cases, where if is expressive enough.ribesg
04/10/2019, 7:44 AMif would be better if we had a ternary operator...