Multiple assignment?


#1

I see I can do this:

  val (a, b) = getThingWithTwoParts()

but not, it seems, this…

  var a = …
  var b = …
  (a, b) = getThingWithTwoParts()

Is that right?

thanks,
Rob


#2

This only works with data classes:

 
data class Foo(val a: Int, val b: String)
val (c, d) = Foo(1729, "Hi")

#3

But there's no way to re-assign to vars like that, correct?

Rob


#4

Not at the moment no, but I thought there where plans after 1.0 for expanding the possibilities, Probably someone from JetBrains can answer that. There are also wishes to ignore values. I think we can expect '_' for that.

The only requirement is not data classes by the way, but having componentN  methods, e.g.

fun main(args: Array<String>) {   val (a, b) = Foo("Hello", "world")   println("$a, $b!") }

class Foo<A, B>(val a: A, val b : B) {
  fun component1() : A = a
  fun component2() : B = b
}