Hullaballoonatic
12/04/2019, 6:59 PMif (cond) foo(a) else foo(b)
or
foo(if (cond) a else b)
karelpeeters
12/04/2019, 7:01 PMHullaballoonatic
12/04/2019, 7:04 PMoverride fun MyRangeClass.toString() = buildString {
if (startIsClosed) append("[") else append("(")
append("$start, $end")
if (endIsClosed) append("]") else append(")")
}
I feel like this is short enough that it doesn't justify a temporary variable, and long enough that it feels crowded as an argumentkarelpeeters
12/04/2019, 7:05 PMMike
12/05/2019, 12:18 AMappend
on it's own, so that I know that regardless of which branch the if
takes, something is getting appended. And the if
is just determining what it is.
But definitely no general rule here.
So append(if(startIsClosed) "[" else ")")
tddmonkey
12/05/2019, 8:37 AMval startBracket = if (startIsClosed) "[" else "("
val endBracket = if (endIsClosed) "]" else ")"
append("${startBracket}${start}, ${end}${endBracket}")
Mike
12/05/2019, 11:19 AMHullaballoonatic
12/05/2019, 9:21 PMoverride fun MyRangeClass.toString(): String {
val startBracket = if (startIsClosed) '[' else '('
val endBracket = if (endIsClosed) ']' else ')'
return "$startBracket$start, $end$endBracket"
}
foregoing the StringBuilder
altogetherHullaballoonatic
12/05/2019, 9:24 PMval startBracket = startIsClosed && '[' || ')'
like you can in python, js, lua, etc, but I understand why you wouldn't want to add falsy
to kotlin