https://kotlinlang.org logo
Title
h

Hullaballoonatic

04/11/2019, 8:15 PM
I imagine this is asked a lot, but when are each of the following more appropriate:
val isFoo: Boolean get() = //foo things
fun isFoo(): Boolean = //foo things
Does the
get
imply inexpensive and static? Does the
fun
imply expensive and dynamic? I prefer to have functions only be verbs. So in practice i'll write the above always with getters, but I wonder if that's confusing or abusive. It could lead someone to constantly grab a property that has to be computed each time, e.g.
class Matrix(private val data: List<Vector>) {
    val rows get() = data
    val cols get() = List(numCols) { j -> Vector(List(numRows) { i -> data[j][i] }) }
}
Wouldn't this imply that
fun foo() {
    for (row in rows) {
        // things
        rows.otherThings()
    }
}
would take the same time to compute as
fun bar() {
    for (col in cols) {
        // things
        cols.otherThings()
    }
}
for you case I would use properties syntax