How to assign the same value to more var in scala - scala

To instantiate the variables can do so:
scala> var (a, b, c) = (0, 0, 23)
a: Int = 0
b: Int = 0
c: Int = 23
but if I wanted to do such a thing?
scala> a = b = c
<console>:10: error: type mismatch;
found : Unit
required: Int
a = b = c
^
how can I do?
Thanks

var a,b,c = 0
should do the trick.

You can't do a = b = c because a has already been defined as a Int var, and with the a = b = c statement you are giving a a Unit, 'b = c'.
When you assign a value to a variable in Scala you don't get as a result the value assigned.
In other languages b = c would be evaluated to 23, the value of c. In Scala b = c is just a Unit, writing a = b = c is exactly like writing a = (b = c), hence the error.

Related

What is the "real" difference between val b and var b?

What is the "real" difference between the following ?
val b = ( x:Double) => x * 3
var b = ( x:Double) => x * 3
Technically speaking, once a value is assigned to val , it should not be changed. However, as part of the first statement, the value of b could be changed to different values by passing different values of x.
scala> val b = ( x:Double) => x * 3
b: Double => Double = $$Lambda$1109/411756754#7a522157
scala> b(3)
res1: Double = 9.0
scala> b(4)
res2: Double = 12.0
What is actually happening here? Is it not that value of b is changing here?
b is a function that takes a Double and returns a one.
The function itself can't be changed, not the value it returns (functions are first class values).
If you try to do:
b = (x : Double) => x * 6
you'll get:
error: reassignment to val
But it's possible to change the var one:
scala> b = (x : Double) => x * 7
b: Double => Double = $$Lambda$1308/1272194712#9e46050
But note that when you change the var one, you should keep its type: A function that takes a Double and returns a Double, the same if you were to change any other type like Integer or Boolean.
In the example posted by you b is a function. You are passing different values to the function subsequently. Value of b is not changed by that.
Syntax is as follows:

Scala closure Lexical Scope

class Cell(var x: Int)
var c = new Cell(1)
val f1 = () => c.x /* Create a closure that uses c */
def foo(e: Cell) = () => e.x /* foo is a closure generator with its own scope */
// f2 wont do any reference/deep copy
val f2 = foo(c) /* Create another closure that uses c */
val d = c /* Alias c as d */
c = new Cell(10) /* Let c point to a new object */
d.x = d.x + 1 /* Increase d.x (i.e., the former c.x) */
// now c.x refers to 10
println(f1()) /* Prints 10 */
println(f2()) /* Prints 2 */
Here the f2() prints 2 , As scala wont do deep copy, why the value is still persisted as 1, it should be 10.. where i am going wrong
2) I had read smomehere, Closure in scala dont deep copy the objects, they just keep reference to the object. what do it exactly mean
Your example is somewhat tough to understand due to the way you copied it in (it looks like all the code is run when a Cell is created, but you'd get infinite recursion if that were true). The reason f1 and f2 return different results is that they are pointing at different Cells. You are right that when you write:
val d = c
both c and d contain the same reference. But when you write:
c = new Cell(10)
c is now a reference to a new cell, and d won't copy over that reference.
It's easier to see this with REPL, which can print hexadecimal reference locations.
scala> class Cell(var x: Int)
defined class Cell
scala> var a = new Cell(5)
a: Cell = Cell#368239c8
scala> val b = a
b: Cell = Cell#368239c8
We can see that a and b contain references to the same cell.
scala> a.x = 10
a.x: Int = 10
scala> b.x
res0: Int = 10
When we update the class referenced by a, it also updates for b.
scala> a = new Cell(7)
a: Cell = Cell#5b87ed94
scala> b
res1: Cell = Cell#368239c8
scala> a.x
res2: Int = 7
scala> b.x
res3: Int = 10
When we assign our variable a to a new cell, it has a different reference location (it is a different instance of Cell). b still has the same reference (why wouldn't it?).

Scala: is it possible to make a method "+" work like this: x + y = z?

I have a graph, with each vertex connected to 6 neighbors.
While constructing the graph and making declarations of the connections, I would like to use a syntax like this:
1. val vertex1, vertex2 = new Vertex
2. val index = 3 // a number between 0 and 5
3. vertex1 + index = vertex2
The result should be that vertex2 be declared assigned as index-th neighbor of vertex1, equivalent to:
4. vertex1.neighbors(index) = vertex2
While frobbing with the implementation of Vertex.+, I came up with the following:
5. def +(idx: Int) = neighbors(idx)
which, very surprisingly indeed, did not cause line 3 to be underlined red by my IDE (IntelliJIdea, BTW).
However, compilation of line 3 offsprang the following message:
error: missing arguments for method + in class Vertex;
follow this method with `_' if you want to treat it as a partially applied function
Next, I tried with an extractor, but actually, that doesn't seem to fit the case very well.
Does anybody have any clue if what I'm trying to achieve is anywhat feasible?
Thank you
You probably can achieve what you want by using := instead of =. Take a look at this illustrating repl session:
scala> class X { def +(x:X) = x; def :=(x:X) = x }
defined class X
scala> val a = new X;
a: X = X#7d283b68
scala> val b = new X;
b: X = X#44a06d88
scala> val c = new X;
c: X = X#fb88599
scala> a + b := c
res8: X = X#fb88599
As one of the comments stated, the custom = requires two parameter, for example vertex1(i)=vertex2 is dessugared to vertext.update(i,vertex2) thus forbidding the exact syntax you proposed. On the other hand := is a regular custom operator and a:=b will dessugar to a.:=(b).
Now we still have one consideration to do. Is the precedence going to work as you intent? The answer is yes, according to the Language Specification section 6.12.3. + has higher precedence than :=, so it ends up working as (a+b):=c.
Not exactly what you want, just playing with right-associativity:
scala> class Vertex {
| val neighbors = new Array[Vertex](6)
| def :=< (n: Int) = (this, n)
| def >=: (conn: (Vertex, Int)) {
| val (that, n) = conn
| that.neighbors(n) = this
| this.neighbors((n+3)%6) = that
| }
| }
defined class Vertex
scala> val a, b, c, d = new Vertex
a: Vertex = Vertex#c42aea
b: Vertex = Vertex#dd9f68
c: Vertex = Vertex#ca0c9
d: Vertex = Vertex#10fed2c
scala> a :=<0>=: b ; a :=<1>=: c ; d :=<5>=: a
scala> a.neighbors
res25: Array[Vertex] = Array(Vertex#dd9f68, Vertex#ca0c9, Vertex#10fed2c, null, null, null)

x = y =1 in Scala?

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?
Thanks
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 = ()
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
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.)
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
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

Scala methods ending in _=

I seem to remember Scala treating methods ending in _= specially, so something like this:
object X { var x: Int = 0; def y_=(n : Int) { x = n }}
X.y = 1
should call X.y_=(1). However, in 2.8.0 RC1, I get an error message:
<console>:6: error: value y is not a member of object X
X.y = 1
^
Interestingly, just trying to call the method without parentheses fails as well:
scala> X.y_= 1
<console>:1: error: ';' expected but integer literal found.
X.y_= 1
^
Am I misremembering something which does actually exist or did I just invent it out of whole cloth?
This is one of those corner cases in Scala. You cannot have a setter without a getter and vice versa.
The following works fine:
scala> object X {
| var x: Int = 0
| def y = x
| def y_=(n: Int) { x = n }
| }
defined module X
scala> X.y = 45
scala> X.y
res0: Int = 45
scala> object X { var x: Int = 0; def y_=(n : Int) { x = n }}
defined module X
scala>
scala> X y_= 1
scala> X.x
res1: Int = 1
scala> object X { var x: Int = _; def y = x ; def y_=(n: Int) { x = n } }
defined module X
scala> X.y = 1
scala> X.y
res2: Int = 1
scala>