Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I express a chained assignment in Scala?

How would I express the following java code in scala?

a = b = c;

By the way, I'm re-assigning variables (not declaring).

like image 251
someguy Avatar asked Mar 14 '10 13:03

someguy


1 Answers

The closest shortcut syntax in Scala can only be used when you declare a var or val.

scala> val c = 1  
c: Int = 1

scala> val a, b = c
a: Int = 1
b: Int = 1

From the Scala Reference, Section 4.1

A value declaration val x1 , ... , xn: T is a shorthand for the sequence of value declarations val x1: T ; ...; val xn: T. A value definition val p1, ..., pn = e is a shorthand for the sequence of value definitions val p1 = e ; ...; val pn = e . A value definition val p1, ... , pn : T = e is a shorthand for the sequence of value definitions val p1 : T = e ; ...; val pn: T = e .

This doesn't work for re-assignement to a var. The C/Java style doesn't work for reasons explained here: What is the Motivation for Scala Assignment Evaluating to Unit

like image 119
retronym Avatar answered Sep 22 '22 03:09

retronym