Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, September 10, 2009

Assignments

Assignment in Scala follows more or less the same rules as Java and other related rules. There are some differences.

Increment operators do not work (i++). As I understand it the rational is that it is too specific an idiom. There is not an easy way to generalize it. Instead you must use i += 1.

Assignments do not return a value. For example you cannot do val i = j = 2 or while( (i = read(buffer)) > 0 ){...}.

One feature that is fairly unique in Scala is the ability to expand a case-class or other class that has an associated extractor. For details look at the previous topic Assignment and Parameter Objects.

Examples:

  1. scala> val i,j=2
  2. i: Int = 2
  3. j: Int = 2
  4. scala> val (i,j) = (1,2)
  5. i: Int = 1
  6. j: Int = 2
  7. scala> val (i,j,k) = (1,"two",3.0)
  8. i: Int = 1
  9. j: java.lang.String = two
  10. k: Double = 3.0
  11. scala> caseclass Data( name:String, age:Int, weight:Float)
  12. defined class Data
  13. scala> val Data(name, age, weight) = Data("Jesse", 133, 100f)
  14. name: String = Jesse
  15. age: Int = 133
  16. weight: Float = 100.0
  17. scala> val value = 1
  18. value: Int = 1
  19. scala> i += 1
  20. :10: error: reassignment to val
  21.        i += 1
  22.          ^
  23. scala> var variable = 1
  24. variable: Int = 1
  25. scala> variable += 1
  26. scala> println(variable)
  27. 2

Monday, August 31, 2009

Java Conversions

The collections APIs in Scala are completely separate from those in Java, the primary reason is because they are designed from the ground up to be functional in nature. However, in keeping with Scala's goal of effortless Java integration there are implicits that convert between Java and Scala collections.

The object that needs to be imported has changed between Scala 2.7.x and 2.8 but the usage is the same. In 2.8 all conversions from Java to Scala objects (be it collections, concurrent constructs or others) are in objects call JavaConversions. For example in the August 31 nightly build there are 2 JavaConversions objects. scala.collection.JavaConversions and scala.concurrent.JavaConversions. As time progresses I expect that the types of JavaConversions will expand.

In scala 2.7 you can not convert Scala collections to Java collections only Java to Scala. In 2.8 you can do both.

So here is how to use conversions in 2.7.x:
  1. scala>val jmap = new java.util.HashMap[String,Int]()
  2. jmap: java.util.HashMap[String,Int] = {}
  3. scala> jmap.put("one", 2)
  4. res0: Int = 0
  5. scala> jmap.get("one") 
  6. res1: Int = 2
  7. scala>import scala.collection.jcl.Conversions._
  8. import scala.collection.jcl.Conversions._
  9. scala> jmap("one")
  10. res3: Int = 2
  11. scala> jmap("one") = 1
  12. scala> jmap("one")
  13. res5: Int = 1


And in 2.8:
  1. scala>val jmap = new java.util.HashMap[String,Int]()
  2. jmap: java.util.HashMap[String,Int] = {}
  3. scala> jmap.put("one", 2)
  4. res0: Int = 0
  5. scala> jmap.get("one")
  6. res1: Int = 2
  7. scala>import scala.collection.JavaConversions._
  8. import scala.collection.JavaConversions._
  9. scala> jmap("one")
  10. res3: Int = 2
  11. scala> jmap("one") = 1
  12. scala> jmap("one")
  13. res5: Int = 1