Showing posts with label operator. Show all posts
Showing posts with label operator. Show all posts
Monday, March 22, 2010
Implicit '=' operator
Continuing on with operators, There is a special type of operator in Scala. It is an operator that ends with =. If a class has operation (methods with an operator identifer) the = can be appended to the effectively creating a new method. In truth a new method is not created instead the compiler rewrites the line.
For example. If a method (like Int) defines + then a method call += can be used. It can be used to mutate a variable:
To illustrate this is not a special case for Int the next example defines several operations and demonstrates in place variable mutation.
Here are several more examples using existing classes in Scala. They are all immutable examples.
Note: assignment operators can also be defined as methods to mutate an object
For example. If a method (like Int) defines + then a method call += can be used. It can be used to mutate a variable:
- scala> var i = 1
- i: Int = 1
- scala> i += 1
- scala> i
- res3: Int = 2
To illustrate this is not a special case for Int the next example defines several operations and demonstrates in place variable mutation.
- scala> case class MyClass(i:Int) {
- | def +(j:Int) = new MyClass(j + i)
- | def -(j:Int) = new MyClass(i - j)
- | def ^(j:Int) = MyClass(j)
- | def +|(j:Int) = new MyClass(j + i / 3)
- | }
- defined class MyClass
- scala> var c = MyClass(1)
- c: MyClass = MyClass(1)
- scala> c+=6
- scala> c
- res5: MyClass = MyClass(7)
- scala> c -= 2
- scala> c
- res7: MyClass = MyClass(5)
- scala> c ^= 10
- scala> c
- res23: MyClass = MyClass(10)
- scala> c +|= 5
- scala> c
- res25: MyClass = MyClass(8)
Here are several more examples using existing classes in Scala. They are all immutable examples.
- scala> var l = Set(1,2,3)
- l: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
- scala> l += 10
- scala> l
- res7: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 10)
- scala> var seq = Seq(5,6,3)
- seq: Seq[Int] = List(5, 6, 3)
- scala> seq :+= 10
- scala> seq
- res9: Seq[Int] = List(5, 6, 3, 10)
- scala> seq +:= 10
- scala> seq
- res11: Seq[Int] = List(10, 5, 6, 3, 10)
- scala> var list = List(32)
- list: List[Int] = List(32)
- scala> list ::= 12
- scala> list
- res13: List[Int] = List(12, 32)
Note: assignment operators can also be defined as methods to mutate an object
- scala> case class MyClass(var i:Int) {
- | def += (j:Int) = { i+=j ; this }
- | }
- defined class MyClass
- scala> val m = MyClass(6)
- m: MyClass = MyClass(6)
- scala> m += 7
- res0: MyClass = MyClass(13)
- scala> m += 9
- res1: MyClass = MyClass(22)
- scala> res1 eq m
- res2: Boolean = true
Labels:
assignment,
intermediate,
operator,
Scala
Friday, March 19, 2010
Operators
Since Scala allows one to define the behavior of operators there are some rules involving operators and assignment like +=. One of the standard method calls in most languages is
Since
These characters can be method names but they cannot be combined with other identifier characters.
Update: These characters can be combined with other identifier characters if there is an under score so:
However these characters are special because they can be combined in a special way with '=' for a special assignment construct as shown in the next post.
(end update)
i += 1.
Since
i+=1
(no spaces) is also valid, Scala has some rules regarding how statements like i+=1
should be broken up. Obviously we know it should be 'i' '+=' and '1'. So there is a special class of characters called operators. I don't know all of them but a few are: + - ^ * / % ! | & =
( ':' is sort of part of this group but has some special properties as well). These characters can be method names but they cannot be combined with other identifier characters.
Update: These characters can be combined with other identifier characters if there is an under score so:
- def x+ = 3 // not valid
- def x_+ = 3 // valid
- def +x = 3 // not valid
However these characters are special because they can be combined in a special way with '=' for a special assignment construct as shown in the next post.
(end update)
- scala> case class MyClass(i:Int) {
- | def +(j:Int) = new MyClass(j + i)
- | def -(j:Int) = new MyClass(i - j)
- | def ^(j:Int) = MyClass(j)
- | def +|(j:Int) = new MyClass(j + i / 3)
- | }
-
- scala> val c = MyClass(3)
- c: MyClass = MyClass(3)
- scala> c + 4
- res26: MyClass = MyClass(7)
- scala> c-2
- res27: MyClass = MyClass(1)
- scala> c -6
- res28: MyClass = MyClass(-3)
- scala> c ^ 3
- res29: MyClass = MyClass(3)
- scala> c+|5
- res31: MyClass = MyClass(6)
Labels:
assignment,
beginner,
operator,
Scala
Subscribe to:
Comments (Atom)