what's the Kotlin equivalent of returning a Rust `...
# getting-started
y
what's the Kotlin equivalent of returning a Rust
&mut
to a map value? something like
Copy code
fun getTarget(where: Where) {
    val targetMap = if (foo) { this.map1 } else { this.map2 }
    return targetMap[where]
}

// then used as something like
getTarget(somewhere) = "hello"
e
there isn't one in general
k
The typical idiom is to return the map itself:
Copy code
fun getTarget() =
    if (foo) { this.map1 } else { this.map2 }

// then used as something like
getTarget()[somewhere] = "hello"
which is not really equivalent. Kotlin, like Java, doesn't have the concept of "LHS" types (such as C++'s or Rust's mutable references).
p
I'd potentially have an updateTarget instead that takes an updater as the final argument that takes and updates the item. &mut is usually short lived so that would probably mimic the same flow
y
thanks everyone. the suggestion by @Pearce Keesling can work here.
y
If
getTarget
is defined on some receiver type, then you can simply rename it to set:
Copy code
object Target {
  operator fun set(where: Where, value: String) {
    val targetMap = ...
    targetMap[where] = value
  }
}
Target[somewhere] = "hello"