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:
  1. scala> var i = 1
  2. i: Int = 1
  3. scala> i += 1
  4. scala> i
  5. 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.
  1. scala> case class MyClass(i:Int) {      
  2.      | def +(j:Int) = new MyClass(j + i)
  3.      | def -(j:Int) = new MyClass(i - j)
  4.      | def ^(j:Int) = MyClass(j)
  5.      | def +|(j:Int) = new MyClass(j + i / 3)
  6.      | }
  7. defined class MyClass
  8. scala> var c = MyClass(1)
  9. c: MyClass = MyClass(1)
  10. scala> c+=6
  11. scala> c
  12. res5: MyClass = MyClass(7)
  13. scala> c -= 2
  14. scala> c
  15. res7: MyClass = MyClass(5)
  16. scala> c ^= 10
  17. scala> c
  18. res23: MyClass = MyClass(10)
  19. scala> c +|= 5
  20. scala> c
  21. res25: MyClass = MyClass(8)

Here are several more examples using existing classes in Scala. They are all immutable examples.
  1. scala> var l = Set(1,2,3) 
  2. l: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
  3. scala> l += 10
  4. scala> l
  5. res7: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 10)
  6. scala> var seq = Seq(5,6,3)
  7. seq: Seq[Int] = List(5, 6, 3)
  8. scala> seq :+= 10
  9. scala> seq                 
  10. res9: Seq[Int] = List(5, 6, 3, 10)
  11. scala> seq +:= 10   
  12. scala> seq       
  13. res11: Seq[Int] = List(10, 5, 6, 3, 10)
  14. scala> var list = List(32)
  15. list: List[Int] = List(32)
  16. scala> list ::= 12
  17. scala> list
  18. res13: List[Int] = List(12, 32)

Note: assignment operators can also be defined as methods to mutate an object
  1. scala> case class MyClass(var i:Int) {
  2.      | def += (j:Int) = { i+=j ; this }
  3.      | }
  4. defined class MyClass
  5. scala> val m = MyClass(6)
  6. m: MyClass = MyClass(6)
  7. scala> m += 7
  8. res0: MyClass = MyClass(13)
  9. scala> m += 9
  10. res1: MyClass = MyClass(22)
  11. scala> res1 eq m
  12. res2: Boolean = true

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 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:
  1. def x+ = 3   // not valid
  2. def x_+ = 3  // valid
  3. 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)

  1. scala> case class MyClass(i:Int) {
  2.      | def +(j:Int) = new MyClass(j + i)
  3.      | def -(j:Int) = new MyClass(i - j)
  4.      | def ^(j:Int) = MyClass(j)
  5.      | def +|(j:Int) = new MyClass(j + i / 3)
  6.      | }
  7.  
  8.  scala> val c = MyClass(3)
  9.  c: MyClass = MyClass(3)
  10.  scala> c + 4
  11.  res26: MyClass = MyClass(7)
  12.  scala> c-2 
  13.  res27: MyClass = MyClass(1)
  14.  scala> c -6
  15.  res28: MyClass = MyClass(-3)
  16.  scala> c ^ 3
  17.  res29: MyClass = MyClass(3)
  18.  scala> c+|5
  19.  res31: MyClass = MyClass(6)