Felix Kranich
01/18/2024, 1:46 PMwhere
blocks to allow adding local bindings to single-expression functions without creating a block (similar to Haskell's where)
fun greet(hourOfDay: Int, name: String) = println(greeting + name)
where {
val greeting = when {
hourOfDay < 10 -> "Good morning, "
hourOfDay < 12 -> "Good day, "
hourOfDay < 17 -> "Good afternoon, "
else -> "Good evening, "
}
}
David Kubecka
01/18/2024, 1:52 PMwhen {
hourOfDay < 10 -> "Good morning, "
hourOfDay < 12 -> "Good day, "
hourOfDay < 17 -> "Good afternoon, "
else -> "Good evening, "
}.let { println(it + name) }
dmitriy.novozhilov
01/18/2024, 2:03 PMFelix Kranich
01/18/2024, 2:14 PMwhere
block should only allow val
bindings, encouraging creation of well-named intermediary valuesdmitriy.novozhilov
01/18/2024, 2:17 PMFelix Kranich
01/18/2024, 2:22 PMdmitriy.novozhilov
01/18/2024, 2:24 PMdmitriy.novozhilov
01/18/2024, 2:24 PMKlitos Kyriacou
01/18/2024, 3:09 PMfun greet(hourOfDay: Int, name: String) = println(greeting(hourOfDay) + name)
private fun greeting(hourOfDay: String) = when {
hourOfDay < 10 -> "Good morning, "
hourOfDay < 12 -> "Good day, "
hourOfDay < 17 -> "Good afternoon, "
else -> "Good evening, "
}
Would you prefer fun greeting
to be declared before its use? Normal Kotlin practice is to define it after first use. So the readability principle of "declaration before use" is not universal.
Having said that, I quite like the use of where
in Haskell - it does greatly improve readability in my opinion - but I don't think it quite fits into intuitive Kotlin syntax.Daniel Pitts
01/18/2024, 4:07 PMAnd in my personal opinion, expression bodies for functions were a mistakeI'm curious what your reasoning is for this. I can certainly see how they can be abused, but when used correctly they can greatly simplify and clarify code.
Javier
01/18/2024, 4:50 PMdmitriy.novozhilov
01/19/2024, 7:59 AMJavier
01/19/2024, 8:03 AMDaniel Pitts
01/19/2024, 4:15 PMLaxystem
01/27/2024, 2:01 PMopen
functions and properties should always have explicit types.dmitriy.novozhilov
01/27/2024, 2:26 PM