Scala in Action Chapter 3 Classes and Objects notes

class MongoDBClinet(val host: String, val port: Int)

val means these values are immutable

Primary constructor - It is called when object is created or called from overloaded constructors

If use var then scala also creates getters and setters
If use val then scala creates only getters , remember val are not changable so no setters

When both val and var are missing the instance values are treated as private and not accessible to anyone outside the class

Section 3.1

Download mongodb from http://www.mongodb.org/downloads

Extract it to some location

Start the mongodb

cd C:\Jagat\tools\mongodb-win32-i386-2.4.9\
mkdir db_data
bin\mongod.exe --db_path=db_data

:http://127.0.0.1:28017/

This will show admin path

The client waits at port  27017

Section 3.2

Classes and Objects

Construct the class with default values for host and port

class MongoClient ( val host : String , val port : Int ) {
def this() = this("127.0.0.1",27017)
}

The first statement in overloaded constructor has to be either other overloaded consturcotr or the primaty constructor.

To do some otherration before you invoke construcotr we use companion objects

Add the mongodb jar driver to class path of REPL session


Add jar to scala REPL classpath

scala> :cp C:\Jagat\tools\scala-eclipse\workspace\scalaination\lib\mongo-java-dr
iver-2.12.0.jar
Added 'C:\Jagat\tools\scala-eclipse\workspace\scalaination\lib\mongo-java-driver
-2.12.0.jar'.  Your new classpath is:

Section 3.3

Packaging

In scala you can have nested packages

package A {

package B {


}


}

You can also use java style packaging with package declared at the top of the file

Scala packaging structure does not have to be matching with the folder structure in file system like Java. But when we compile the classes the required folder structure is auto generated as JVM needs that for working. Remember under the hood scala is running on the top of JVM

To add the jar in scala classpath via command line

scalac -classpath my.jar MyNewClass.sclaa

Section 3.4

Scala imports

You can add import statement at any point in the code.

The import will be visible lexically in the code.

* equivalent of java is _ in scala

To import all use

my.package._

If you declare classes or objects without any package , then they all belong to empty package. The cannot be imported to any other package. But members of empty package can see each other

Remap package scala class to avoid conflicts

import java.sql.{Data => SqlDate }

Hide a class , the Date class below cannot be used

import java.sql.{Data => _ }

Section 3.5

Objects and companion objects

There is no static variables in scala

Scala supports concept of having companion object and classes.

bstract class Role {

  def canAccess(page: String): Boolean
}

class Root extends Role {
  override def canAccess(page: String) = page != "Admin"

}

object Role {

  def apply(roleName: String) = roleName match {
    case "root" => new Root
    case "analyst" => new Root
  }
}

Note the object Role and class Role in above code , they are companion object and companion class

Package objects

Package objects allow you to define something at central place which can be used by all the memebers of the package

They are generally defined in file named

package.scala , in the package that corresponds to it

package object ch3 {
 
  val minAge = 18

}

This above variable can be used anywhere in this package named ch3



No comments:

Post a Comment

Please share your views and comments below.

Thank You.