Showing posts with label Scala. Show all posts
Showing posts with label Scala. Show all posts

Sunday, September 15, 2013

Scala : Packages,Imports and Access modifers

Packages in scala are similar to java but a lot more flexible and at a higher level. By higher level I mean a developer can organize the packages in a program from his/her logical view and do not have to worry about the lower level folder structure in which JVM requires the packages to be organized.
Once again scala provides an abstraction and flexibility that would be nice to have in Java.
so let us say I want to define a package coms.abc.depts and com.abc.depts.employee.
Every java programmer knows how to do this in Java. Only by hte naming convention we indicate that these two packages in fact are related, but there is no other way to organize them like the scala code below.

package coms {
  package abc {
    package depts {
      class Dept(val deptName: String, val deptId: Int) {

        def findNumOfEmployees(): Int =
          {
            var nEmps = 0;
            nEmps;
          }

      }

      package employee {

        class Employee(val ssn: Int, val name: String) {

        }
      }

    }
  }
}

This code of course will be organized into two different directories for java classess.

When we define a package, of course, we have to talk about how to import the package.Here again scala provides a way to import a package where it is required making the code much more easy to read and less cluttered at the top. every java programmer who has to use java's date and java.sql.Date can quickly appreciate it.
Let us see an example that shows the versatility of the import statements. The code also shows how the constants can be defined, imported and used. Most apps we have at least one list of constants, the public static finals. Scala implements pure oops, and therefore does not have any static declarations.
Here is how we will define constants in Scala.

package org.manu.blog.constants
  object MyContext{
    val  appName = "Manu Blog"
    val  banner = "Welcome to Manu's Blog"
    val mission ="Our mission is to explore the Human origins"
    val end ="Mission accomplished"
  }

Now to use it, this is how we will import the constants as well as the dept and employee we defined.
package org.manu.test

object Test11 {
  def main(args: Array[String]) {
    employees
    def employees = {
      import org.manu.blog.constants.MyContext._
      println("welcome to " + appName + "\n" + mission)

      import coms.abc.depts.Dept
      val dep = new Dept("my Dept", 10)
      import java.math.BigDecimal, BigDecimal._
      println("Employees in my dept :" + dep.findNumOfEmployees);
      println("Our CEO currently draws a salary of "+ ZERO)

      println(end)
    
    }
  }
}

The points worth noting are:
1. Once we import a package/class, now we can use its contents thereafter.
    That is how we are using the appName and end strings.

2. The scala style packages we defined for dept and employee have the same nomenclature as java, i.e. coms.abc.dept

3. The _ which works like * in Java for imports, is applicable also to a Object. This is what the declaration is : import org.manu.blog.constants.MyContext._
This declaration essentially means that we can now use all the members object MyContext directly with their name. i.e. We can do not have to access end as MyContext.end any more.  

4. Java's static can also be imported in scala. That is how we are using ZERO of BigDecimal in the statement. The statement import java.math.BigDecimal, BigDecimal._ is how it is done, simialr to the one in 3 above.

In a nutshell, I like the scope style declaration of the packages and find it much more intuitive for nested and closely related package declarations.
I think for the packages that are mostly used in a class, I will like to import those in java style, at the top of the class file. As we know there are many time packages that are used may be only once or twice in a class and in such situations I find the scala style import very useful and intuitive as well.

Access Modifiers

Private:
With packages, the other appropriate discussion is about the access modifiers.
The thing I like a lot is the Default is PUBLIC. How many times do we really declare a class that is NOT public. I think this is a great improvement.

So the default is public and private is java like private when declared inside a class. Now just like java static the private of a class is also available to its companion class which we will use to declare the static members of a class.
Now let us say we want ot hide the findNumbers of the department class and instead provide a static method to show the numbers. (not a great example but works for the concept).

Let us make the corresponding changes to the Dept class:

 class Dept(val deptName: String, val deptId: Int) {

        private def findNumOfEmployees(): Int =
          {
            var nEmps = 0;
            nEmps;
          }

        def companyName(): String = {
          Dept.company
        }

        object Dept {

          val company = "ABC Company"

          def showNumbers(): String = {

            company + " : " + deptName + findNumOfEmployees

          }

        }

      }

Points to note:

1. The private method findNumuOfEmployees can be used directly in the companion object.
2. The company declared in the companion object can be in turn used int the any class method of the class of the companion object.t

This is the way to call these from any other object:

      import coms.abc.depts._
      val dep = new Dept("Motor Vehicle ", 10)
      println( "show Numbers: " + dep.Dept.showNumbers() );


Private and Protected Scopes

Private can be further qualified with a package, let us says we have a method in Employee that returns if an employee is a contractor, the Dept class might have a method to count all its contractors. We therefore want this method exposed to the Dept package classes but not any other classes.
To achieve this we will put a qualifier on this method as follows.

package employee {

         class Employee(val ssn: String, val name: String) {
         
         private[depts] def isContractor() : Boolean = { true}

        }
      }


Now we can use this method only inside the depts classes as  below.
def findContractors(){
          import coms.abc.depts.employee._
          var emp = new Employee("55", "Unknown")
          emp.isContractor;
       }
However we cannot use it any other package. Similarly we can use protected with a qualifier.

The java visibility and scala visibility works as follows

no modifer public access, any class in any package can access
private[depts] access within enclosing /outer package depts (all classes)
private[employee] only inside the package employee, the default access of java
private[Employee] same as private in Java








Sunday, August 4, 2013

How I decided to learn Scala?

I started coding java in 1999 when I was working at the Sun Microsystems. It was the time when servlets was the newest and hottest technology. Before that I was developing perl-cgi and was  feeling the limitations of the perl paradigm and wanted to shift to a new paradigm, and oops felt the great option.

All these years,  I never felt comfortable with Java Threading model though, for concurrency coding the developer has to delve in lower levels, e.g. to set up thread priority you have to know how the operating system works, implementation of ++ operators, multi-core vs single core configurations.  I was also writing
 way too much boilerplate code, even the code for a simple bean is too bulky with all its getters and setters. The tools can help generate the code, but the classes are still too bulky. Six months ago I was writing a piece of code for a DSL, the idea was to use business analyst written methods, it was not that easy coding it in Java, Groovy made it work faster.


Considering all this, I thought it was time to explore other languages. I have of course been hearing a lot about Scala, and felt like it would be the best choice, but I wanted to make sure that it really was the best option for me. At the same time one of my friends  had become a champion for learning Ruby and another friend was complaining about the performance limitations he hit with JRuby for a large application. I was therefore looking for some objective information on the popular languages, and Walla, there it was 

Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Pragmatic Programmers) 

The book covered Ruby, Io, Prolog, Scala, Erlang, Clojure and Haskell.
From the book, one of the best books I read in a while, I slowly stated to come to a conclusion:

Ruby is all about the programmer productivity, very flexible, (open classes, missed method),object oriented with functional constructs,  and would be great to bring a product very quickly to the market. The concurrency is not very evolved. Duck typing is good, but absence of Static Typing can be a problem for IDEs and refactoring tools, and I am almost addicted to these tools, cannot imagine development without them.  I also gathered that my friend's experience about Ruby performance is not all that unusual, rather expected.
Io, a purely prototyping language, very small syntax, no syntactic sugar, reminded me of my graduate work using Lisp. On one hand, you don't have to spend a lot of time in learning the syntax, but on the other hand, you have to understand many concepts, did not find a large community. Helped me understand some Javascript concepts better, I don't really develop much in Javascript.
Prolog, I particularly enjoyed reading on Prolog a lot, nostalgia from my graduation thesis, which was in the area of Indian music recognition. Of course Polog is  the best choice when you can model your domain in rules, particularly for artificial intelligence. The best way to use Prolog would be inside a main application. It is not based on JVM.
Erlang, described in three words is Concurrency, Concurrency and Concurrency. It is also extremely reliable, since  reliability was a major requirement for Ericsson labs' Telecom applications. For a java Developer like me, it would be a totally new game. I am not sure about the community support and how easy it will be for me to debug, dig into language features.
Clojure, what I gathered from the book is very powerful and flexible. From the syntax and examples I thought that to exploit its power, one has to spend a long time in learning the language, and then I don't know how easy it would be to find a bunch of such programmers to build a project.
Haskell, a pure functional paradigm, it is not that easy for some one like me to shift my thinking from object oriented style. I think I would have to spend more time in design with Haskell, for most domains object oriented design fits so well. 

In a nutshell, I would learn any one of the languages mentioned above only if the app I want to develop warrants for it. E.g. To bring a web product very quickly to the market I will probably go for Ruby, to develop an extremely reliable, concurrent product I will think of using Erlang, for an artificial intelligence product, Prolog probably would be my first choice.

Right now I want to learn a language that I can learn rather quickly, with semblance of Java, with less boilerplate code, less bulky code, easier to understand and implement concurrency features, can use existing Java libraries and even home-grown code like JPA, JDBC etc, essentially runs on JVM, can let me think in OO paradigm, and yes Scala fits the bill.
Scala has coexisting functional and oo paradigms, it is easier to learn for a Java programmer compared to learning Haskell or Erlang, it is a very general purpose language, facilitates DSL, its static typing lets me use the IDEs and refactoring tools.

Once I came to this conclusion, I started looking for some good resources to learn Scala. It turned out that although comparatively shorter learning curve, Scala too needs some serious reading, and trying out programs. I have finally settled on the book, Scala in Action by Nilanjan RayChaudhuri.

I want to share my journey with scala on this blog. In the next post, I plan to cover the basic features of Scala.