Scala multiple assignment to existing variable
Asked Answered
B

5

10

I can do something like

def f(): Tuple2[String, Long] = ...
val (a, b) = f()

What about if the variables are already existing? I'm running the same sets of data over filters and I don't want to chain them (long names and such). This is what I tried, but it complains about expecting ; instead of = on the last line:

var a = ...initialization for this data
var b = ...some other init
(a, b) = g(a, b) // error: expected ';' but found '='

Is there a way to avoid an intermediary tuple?

Bozovich answered 27/7, 2010 at 23:19 Comment(3)
I don't have a source handy, but I remember reading somewhere that multiple var assignment like that is a bad idea and isn't supported in scala. This was a few months back (and who knows when the original was written), but my understanding is that you need an intermediary.Permafrost
possible duplicate of Scala Tuple DeconstructionAllanite
Possible duplicate of Assign multiple variables at once in scalaHyson
C
14

As Alex pointed out, the short answer is no. What's going on with your code is this: when a and b are already bound variables in the current scope, (a, b) means "take the values of a and b and construct a tuple from them."

Therefore,

(a, b) = ...

is equivalent to

(new Tuple2(a, b)) = ...

which is obviously not what you want (besides being nonsensical).

The syntax you want (ability to assign to multiple variables at once) simply doesn't exist. You can't even assign the same value to multiple preexisting variables at once (the usual syntax "a = b = ..." that's found in many other languages doesn't work in Scala.) I don't think it's an accident that vals get preferential treatment over vars; they're almost always a better idea.

It sounds like all of this is taking place inside of a loop of some kind, and doing repeated assignments. This is not very idiomatic Scala. I would recommend that you try to eliminate the usage of vars in your program and do things in a more functional way, using the likes of map, flatMap, filter, foldLeft, etc.

Consultation answered 28/7, 2010 at 0:55 Comment(2)
The Scala syntax for multiple variable assignment is var a,b,c = 0 It uses commas, not multiple equals signs.Aircondition
Read what I said again. I was talking about assigning to multiple pre-existing variables in one statement, not declaring new variables with initial values. The former is what the OP is looking for, and what is not supported by Scala (to my knowledge).Consultation
A
9

Short answer: No.

Approve answered 27/7, 2010 at 23:32 Comment(0)
P
1

It works for new values because that syntax is treated as a pattern matching, just like case statements. So, as Alex said, you cannot do it.

Pharyngitis answered 28/7, 2010 at 0:31 Comment(0)
C
0

If you run always "the same sets of data" over filters etc, it is a symptom that they belong somehow together, so you should consider to group them using either a tuple or a dedicated class (usually a case class in such cases).

Cuirassier answered 28/7, 2010 at 6:40 Comment(0)
H
0

The workaround I found is this:

// declare as var, not val
var x = (1,"hello")  
// x: (Int, String) = (1,hello)

// to access first element
x._1
// res0: Int = 1

// second element
x._2
// res1: String = hello

// now I can re-assign x to something else
x = (2, "world")
// x: (Int, String) = (2,world)

// I can only re-assign into x as long the types match
// the following will fail

x = (3.14, "Hmm Pie")
<console>:8: error: type mismatch;
 found   : Double(3.14)
 required: Int
       x = (3.14, "Hmm Pie")
Humfrey answered 21/9, 2014 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.