x = y =1 in Scala?

scala tuple
scala cheat sheet pdf
scala constructs
scala operator
scala map
introduction scala
scala built-in functions

While going through the book Scala for the Impatient, I came across this question:

Come up with one situation where the assignment x = y = 1 is valid in Scala. (Hint: Pick a suitable type for x.)

I am not sure what exactly the author means by this question. The assignment doesn't return a value, so something like var x = y = 1 should return Unit() as the value of x. Can somebody point out what might I be missing here?


In fact, x is Unit in this case:

var y = 2
var x = y = 1

can be read as:

var y = 2
var x = (y = 1)

and finally:

var x: Unit = ()

Scala Cheatsheet, Anonymous function: to use an arg twice, have to name it. (1 to 5).map { x => val y = x * 2 println(y) y }, Anonymous function: block style returns last expression. Exercise 3. Come up with one situation where the assignment x = y = 1 is valid in Scala. Since in scala an assignment does return nothing (vs C-like languages), x needs to be of type Unit

You can get to the point of being able to type x=y=1 in the REPL shell with no error thus:

var x:Unit = {}
var y = 0
x = y = 1

Basics | Tour of Scala, You can also name functions: val addOne = (x: Int) => x + 1 println(addOne(1)) // 2. Run. A function can have multiple parameters: val add = (x: Int, y: Int) => x + y  Immutability is // a good thing. val x = 10 // x is now 10 x = 20 // error: reassignment to val var y = 10 y = 20 // y is now 20 /* Scala is a statically typed language, yet note that in the above declarations, we did not specify a type. This is due to a language feature called type inference.

Here’s another less known case where the setter method returns its argument. Note that the type of x is actually Int here:

object AssignY {
  private var _y: Int = _
  def y = _y
  def y_=(i: Int) = { _y = i; i }

import AssignY._

var x = y = 1

(This feature is used in the XScalaWT library, and was discussed in that question.)

Scala examples, University of Iowa Instructor: Cesare Tinelli */ /* Scala examples seen in class the variable def f2 (x:Int, y:Int) = x match { case `y` => x + 1 // will match if x has  Programming in Scala. Return to chapter index 16 Working with Lists. Sample run of chapter's interpreter examples 16.1 List literals; 16.2 The List type;

BTW if assigning of the same value to both variables still required then use:

scala> var x@y = 1
x: Int = 1
y: Int = 1

scala-for-the-impatient/Ex03.scala at master · BasileDuPlessis/scala , Come up with one situation where the assignment x = y = 1 is valid in Scala. (Hint​: Pick a suitable type for x .) */. object Ex03 extends App {. var y:Int = 0. val x:  This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.31, “How to Use a Range in Scala” Problem. You want to see different ways to use a Range in a Scala application.

It's valid, but not sensible, this make confusion.

scala> var x=y=1
x: Unit = ()

scala> y
res60: Int = 1

scala> var x@y = 1
x: Int = 1
y: Int = 1

The Science of Functional Programming (draft version), The result is a sequence that is 1 element shorter than ps , we can compute maps pairs of integers (x, y) to x * y scala> val s = List(1, 2, 3).map(x => List(1, 2,  Scala Operator, Scala Operators, Scala and or not operators, bitwise, arithmetic, assignment operators in scala, modulus, XOR, logical operator in scala.

Scala Language, scala> var x = 1 x: Int = 1 scala> x = 2 x: Int = 2 scala> x = "foo bar" scala> val y = 1 y: Int = 1 scala> y = 2 <console>:12: error: reassignment to val y = 2 ^. class C(var x: R) { assert(x > 0, "positive please") var y = x val readonly = 5 private var secret = 1 def this = this(42) } Constructor is class body. Declare a public member. Declare a gettable but not settable member. Declare a private member. Alternative constructor. new { } Anonymous class. abstract class D {

Scala Cookbook: Recipes for Object-Oriented and Functional Programming, But there are still times when you'll want to use scalac, fsc, scaladoc, and other x: Int = 1 y: Int = 1 scala> x + y res0: Int = 2 scala> val a = Array(1, 2, 3) a:  Re: X-Y scale. There is a way of doing it but not by a single command. Create a block of. the various items making sure that Scale uniformly is *not* ticked and that. Allow exploding *is* ticked.

Programming Scala: Scalability = Functional Programming + Objects, We'll use functions that construct Strings: scala> val fnacLeft = (x: String, y: a list of Strings scala> list2 reduceLeft fnacLeft res2: String = ((((1)-(2))-(3))-(4))-(5)  I need to be able to SCALE at different rates in the X and Y directions. I thought that STRETCH might work but it isn't what I was looking for. Seems like scaling with non-equal amounts would be something I can do

  • I strongly suspect that the author was not thinking of x as of Unit type - that by "valid", they meant to imply "and have some purpose".
  • So the whole point of this question is to show that var x = y = 1 is valid?
  • That's what I had in mind, except that I would have written var x = ()
  • Clearly, this is not what I had in mind :-) But if you want weird solutions, how about implicit def unit2int(u: Unit) = 42; var x = 1; var y = 2; x = y = 1