Example 1: Assigning a single field
- //  Java
- import java.io.File
- /** 
- No real logic behind class.  But for some reason it needs the path of a tmp directory in the working directory
- */
- class OneAssignment {
-   final String field;
-   public OneAssignment() {
-     File file = new File("tmp");
-     if(!file.exists()) {
-       file.mkdirs();
-     }
-     field = file.getAbsolutePath();
-   }
- }
In Scala the naive way to port this would be:
- //  Scala
- import java.io.File
- class OneAssignment {
-   val file = new File("tmp")
-   if(!file.exists()) {
-     file.mkdirs()
-   }
-   val field = file.getAbsolutePath()
- }
Problem is that it has an extra field "file" now. The correct way to port this would be as follows:
- //  Scala
- import java.io.File
- class OneAssignment {
- /* 
- notice that assignment is in a block so file is only visible within the block
- */
-   val field = {
-     val file = new File("tmp")
-     if(!file.exists()) {
-       file.mkdirs()
-     }
-     file.getAbsolutePath()
-   }
- }
Example 2: Assigning multiple fields
- //  Java
- import java.io.File
- /** 
- Basically the same as last example but multiple fields are assigned
- Notice that 2 fields depend on the temporary file variable but count does not
- */
- class MultipleAssignments {
-   final String tmp,mvn_repo;
-   find int count;
-   public OneAssignment() {
-     File file = new File("tmp");
-     if(!file.exists()) {
-       file.mkdirs();
-     }
-     tmp = file.getAbsolutePath();
-     count = file.listFiles.length;
-     
-     File home = new File(System.getProperty("user.home"));
-     mvn_repo = new File(home, ".m2").getPath();
-   }
- }
The Scala port:
- //  Scala
- import java.io.File
- class MultipleAssignments {
- /*
- When multiple fields depend on the same temporary variables the fields can be assigned together from one block by returning a tuple and using Scala's matching to expand the tuple during assignment.  See previous topics on assignment for details 
- */
-   val (tmp,count) = {
-     val file = new File("tmp");
-     if(!file.exists()) {
-       file.mkdirs();
-     }
-     val tmp = file.getAbsolutePath();
-     val count = file.listFiles.length;
-     (tmp, count)
-   }
-   val mvn_repo = {
-     val home = new File(System.getProperty("user.home"));
-     new File(home, ".m2").getPath();
-   }
- }
In some ways the Scala port is cleaner in that it splits the constructor up and decouples the dependencies between fields.
 
So, when will the Daily Scala book be out? :-)
ReplyDeletenow don't be giving anyone idea ;-)
ReplyDeleteIn the first Scala example, is there some reason you used 'def file = new File("tmp")' instead of 'val file = ...'?
ReplyDeleteThat is a mistake. It should be val
ReplyDeleteok post updated to fix def file=
ReplyDeleteBook? I'd buy one :-)
ReplyDeleteCheers
Stephan
http://codemonkeyism.com