Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-Value Assignment Behavior in Scala

Tags:

java

scala

When Scala evaluates the following multiple value assignment:

val a, b, c = UUID.randomUUID()

I expected a == b && b == c && c == a (A) or a == null && b == null && c == UUID (B)

However, the result is a != b && b != c && c != a (C), where each value is initialized to an individual UUID.

Why is this? Java behaves like (B). I believe that (C) is more convenient, however I have not been able to find an explanation in the language doc.

like image 464
Jared Hooper Avatar asked Nov 20 '25 04:11

Jared Hooper


1 Answers

From the scala language reference section 4.1:

A value definition val p1, ..., pn = e is a shorthand for the sequence of value definitions val p1 = e; ...; val pn = e.

So your example is shorthand for:

val a = UUID.randomUUID()
val b = UUID.randomUUID()
val c = UUID.randomUUID()
like image 175
Joe K Avatar answered Nov 22 '25 16:11

Joe K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!