create a script Instructions: xsbt wiki Descend in directory where you wanna create the project In terminal type sbt Once the sbt is started enter following commands set name := “ScalaKnolx” set version := “1.0” set scalaVersion := “2.9.1” session save exit Open build.sbt and have a look!!
build.sbt resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" libraryDependencies += "org.scalatest" %% "scalatest" % "1.6.1" libraryDependencies += "junit" % "junit" % "4.9" Create project/plugins.sbt and add sbteclipse plugin addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.0.0") Execute sbt eclipse It will generate eclipse related configs and now we are ready to import this project in eclipse!!
can be inferred Less Boilerplate Scala> var capital = Map(“US” → “Washington”, “France” → “Paris”) Capital: Map[String, String] = Map(US-> Washington, France->Paris) Scala> capital += (“japan” → “Tokyo”) Scala> capital(“France”) Res2: String = Paris
Int) = { result } def fun = result Scala variable definitions var x: Int = expression val x: String = expression Java method definitions Int fun(int x) { return result } (no parameterless methods) Java variable definitions Int x = expression final String x = expression
Sample(x: Int, p: Int) { def instMeth(y: Int): Int = x + y } object Sample { def staticMeth(x: Int, y: Int): Int = x * y } Java method definitions class Sample { private final int x; public final int p; Sample(int x, int p) { this.x = x; this.p = p; } int instMeth(int y) { return x + y; } static int staticMeth(int x, int y) { return x * y; } }
var field = “!” def abstractMth(x: Int): Int def concMth(x: String) = x + field } Scala mixin composition class C extends Super with T Java Interface Interface T { Int abstractMth(String x) } (no concrete methods) (no fields) Java extension plus implementation class C extends Super implements T
case keyword to each class that is to be pattern matchable Similar to switch except that Scala compares objects as expressions getInteger(4) match { case 4 => println("four") case _ => println("not four") } def getInteger(x: Int): Int = { x }
Scala They encapsulates method and field definitions, which can be reused by mixing them in classes Unlike class inheritance a class can mix any number of traits Unlike Interfaces they can have concrete methods
aString: java.lang.String = hello world scala> aString map (_.toUpper) res1: String = HELLO WORLD Even String is a collection that means that we can apply higher order functions on it
one of its sub- packages mutable, immutable and generic Root collections are in scala.collection define same interface as immutable collections and mutable collections add some modification operations to make it mutable The generic package contains building block for implementing collections
every collection can be created by same uniform syntax Set(1, 2, 3) Seq(1, 2, 3) Traversable(1, 2, 3) Map(“x” → 24, “y” → 25) Applies with specific collection implementations List(1, 2, 3) HashMap(“x” → 24, “y” → 25) All these collections get displayed with toString in same way
constructs Language does not contain any collection related constructs - no collection types - no collection literals - no collection operators Everything is in library They are extensible
Java platform With ScalaTest we can test either Scala or Java code Integrates with popular tools like jUnit, TestNG, Ant, Maven and SBT Designed to do different styles of testing like Behavior Driven Design for example
A test is anything which has a name and can succeed or fail Runner: ScalaTest provides a runner application and can run a suite of tests Reporter: As the tests are run, events are fired to reporter, it takes care of presenting results back to user
in Scala Test you basically invoke run(Option[String], Reporter, …) on Suite object It then calls runNestedSuites(Reporter, …) And it calls runTests(Option[String], Reporter, …) runNestedSuites(Reporter, …): Invokes nestedSuites(): List[Suite] to get a list of nested suites runTests(Option[String], Reporter, …) will call def testNames: Set[String] to get set of test names to run. For each test it calls runTest(Reporter, …) It wraps the test code as a Function object with a name and passes it to the withFixture(NoArgTest) which actually runs the test
and define test methods Test methods have names testXXXX. All methods must be public Scala Test provides === operator. It is defined in Trait Assertions. Allows the failure report to include both right and left values
“test” is a method defined in FunSuite Trait. Test name goes in parentheses and test body goes in curly braces The test code in curly braces is passed as a by-name parameter to “test” method which registers for later execution
Make a Collection hierarchy to hold the above information Write method on the collection hierarchy to get countries of a continent Write method on the collection hierarchy to get continent for a country Write tests using FunSuite to test the methods created above