Andy Victors
08/29/2019, 9:44 PMif (c) a else b
) than the classic one (a ? b : c
) it's still a branching (#if-considered-harmful), it's awkward to write and it is hard to use with nesting. Why not to provide a functional programming style operator eitherOr(c, a, b)
, implemented as a function:
fun <T>eitherOr(condition: Boolean, first: T, second: T) : T {
return if (condition) first else second
}
After all, we already have things like min(a,b)
, `or(a)`etc.SiebelsTim
08/29/2019, 9:54 PMcorneil
08/29/2019, 9:57 PMcorneil
08/29/2019, 9:58 PMinline
as well.SiebelsTim
08/29/2019, 10:01 PMAndy Victors
08/29/2019, 10:04 PMgildor
08/30/2019, 1:39 AMAndy Victors
08/30/2019, 6:58 AMif (a < b) a else b
but why do we have min(a,b)
?gildor
08/30/2019, 7:17 AMmin
has semantics, also reads very well. min from a and b
eitherOr have too abstract semantics.
I have nothing against function that hides condition: Boolean
argument, it’s very handy for sure, but I don’t see how eitherOr would be better, extracting condition to a function tho makes sense, for example such function as getOrElse/getOrPut etcelizarov
08/30/2019, 10:43 AMmin
is because of DRY. If you rewrite min(a, b)
as if (a < b) a else b
, then you have to repeat both a
and b
twice. Not good. Good developers do not repeat themselves.Mike
08/30/2019, 11:42 AM?:
. What you have referenced is the Ternary operator. They are not the same thing, and Kotlin does not have the Ternary operator.
The Elvis operator is only for null checking.
if(a == null) b else a
a ?: b
Those are equivalent statements.Andy Victors
08/30/2019, 9:30 PMcedric
09/03/2019, 6:57 PMif
is considered harmful, hiding it behind one level of indirection is not exactly making it harmless 🙂DALDEI
09/05/2019, 11:34 AMDALDEI
09/05/2019, 11:35 AM