SlideShare una empresa de Scribd logo
1 de 53
Descargar para leer sin conexión
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Masterseminar Scala
Fabian Becker
FH Giessen-Friedberg
15. November 2010
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Content
1 Packages & Imports
2 Assertions and Unit Testing
3 Case Classes and Pattern Matching
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Packages
Packages
Important to minimize coupling
Scala code resides in Java global hierarchy of packages
Per default code is in the unnamed package
There are two ways to place code inside packages
Listing 1.1
package database.mysql
class Query
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Packages
Listing 1.2
package database {
package mysql {
class Query
}
}
Listing 1.3
package database.mysql {
class Query
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Packages
Packages
Packages in Scala truly nest
In Java packages are in a hierarchy they don’t nest
Naming a package in Java you always start at the root
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Packages
Listing 1.4: Accessing packages
package game {
package ui {
class Player
}
package engine {
class Main {
// Java: new game.ui.Player()
val player = new ui.Player
}
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Packages
Listing 1.5: More complex
package ui { class Player } // In ui.scala
package game { // In game.scala
package ui { class Player }
package engine {
package ui { class Player }
class Main {
val player = new ui.Player
val player2 = new game.ui.Player
val player3 = new _root_.ui.Player
}
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Imports
Importing packages
Importing packages allows to directly access members in that
package
Scala imports are similar to the imports from Java
Imports can appear anywhere
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Imports
Listing 1.6.1: Defining Enemies
package game
abstract class Enemy(
val name: String,
var lvl: Int
)
object Enemies {
object Dwarf extends Enemy("Gimli", 10)
object Orc extends Enemy("Bwarg", 5)
object Necromancer extends Enemy("Destructor", 99)
val col = List(Dwarf, Orc, Necromancer)
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Imports
Listing 1.6.2: Importing Enemies
// Simple import for Enemy
import game.Enemy
// Import all members of game
import game._
// Import all members of Enemies
import game.Enemies._
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Imports
Listing 1.7: Import in a function
def showEnemy(enemy: Enemy) {
import enemy._
println("Enemy - Name:"+name+" Level:"+lvl)
}
Imports
Imports can appear in functions
They import all members of a object
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Selective Imports
Listing 1.8: Selective Imports
// Only import Dwarf and Orc
import game.Enemies.{Dwarf, Orc}
// Renaming an import
import game.Enemies.{Necromancer => Necro}
// Rename one, import the rest
import game.Enemies.{Orc => O, _}
// Exclude an import
import game.Enemies.{Orc => _, _}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Selective Imports
Selective Imports
Import: import game.Enemies.{Orc} ⇔ game.Enemies.Orc
Rename: originalname ⇒ newname
Hide Packages: original − name ⇒
Catch All: importgame.Enemies.{ } ⇔ game.Enemies.
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Implicit imports
Listing 1.9: Implicit imports
import java.lang._ // everything in the java.lang package
import scala._ // everything in the scala package
import Predef._ // everything in the Predef object
Implicit imports
Packages imported implicitly by Scala
scala. overshadows java.lang.
scala.StringBuilder ⇒ java.lang.StringBuilder
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Access modifiers
Listing 1.10: Private modifiers
class Outer {
class Inner {
private def f() { println("f") }
class MostInner { f(); // OK }
}
(new Inner).f() // fails
}
Private
Scala: private members are only accessible inside the class or
object that contain their definition
Java allows outer classes to access private members of their
inner classes
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Access modifiers
Protected
Java: protected members are accessible within the same
package
Scala: Only from the object they are defined in and their
subclasses
Public
There is no explicit modifier for public
All members not labeled private or protected are public
Accessable from anywhere
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Access modifiers
Scope of protection
Scala allows to control access to a level X
private[X] and protected[X] allow access up to X
Listing 1.11: Example
package game {
package ui {
private[game] class Player {
private[this] var hp = 42
protected[ui] def getHP() { hp }
}
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Assertions and Unit Testing
Testing Code
Assertions for run-time checking
Unit testing for development testing of small units
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Assertions
Listing 2.1: Assert
def add1(x: Int) : Int = {
val z = x + 1
assert(z > x)
return z
}
println(add1(41))
Assertion methods
Method assert predefined in Predef
assert(condition)
assert(condition, explanations)
If the condition doesn’t hold, an AssertionError is thrown
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Assertions
Listing 1.13: Ensuring
def lvlUp(e: Enemy): Enemy = {
if(e.lvl < 100) {
e.lvl = e.lvl + 1
} ensuring(e.lvl < 100)
e
}
Ensuring
Method assert predefined in Predef
assert(condition)
assert(condition, explanations)
If the condition doesn’t hold, an AssertionError is thrown
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
ScalaTest
org.scalatest.Suite (http://www.scalatest.org/)
Can be executed on the Scala console: (new
HitchhikerSuite).execute()
Listing 2.3: Simple testcase
import org.scalatest.Suite
class HitchhikerSuite extends Suite {
def testAnswerToLife() {
val answer = 42
assert(answer == 42)
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
ScalaTest with Fun
ScalaTest allows different styles of testing
FunSuite is a trait that overrides execute and allows to define
tests as function values
Naming the tests is easier (you can use Strings)
Listing 2.4: FunSuite
import org.scalatest.FunSuite
class HitchhikerSuite extends FunSuie {
test("The answer to life should be 42") {
val answer = 42
assert(answer == 42)
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
ScalaTest - Informative testing
A normal assertion produces long error messages but do not
show why the match failed
ScalaTest defines the === operator for assert(), shows the
values
Alternative: expect(value) { block }
Listing 2.5: Informative testing
assert(answer === 42)
// or
expect(42) {
answer
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
ScalaTest - Catching Exceptions
Testing for Exceptions can be done with intercept()
Listing 2.6: Intercept
s = "hi"
intercept[IndexOutOfBoundsException] {
s.charAt(-1)
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
JUnit
Most popular test framework for Java
Can easily be used from Scala
Does not natively support Scala’s assertion syntax
Listing 2.7: JUnit Testcase
import junit.framework.TestCase
import junit.framework.Assert.assertEquals
import junit.framework.Assert.fail
class MyTestCase extends TestCase {
def testAnswer() {
val answer = 42
assertEquals(42, answer)
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
JUnit + Scala
ScalaTest defines JUnit4Suite (requires JUnit 4 of course)
JUnit4Suite extends TestCase
Listing 2.8: Using Scala syntax in JUnit
import org.scalatest.junit.JUnit4Suite
class AnswerSuite extends JUnit4Suite {
def testAnswerToLife() {
var answer = 42
assert(answer === 42)
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
TestNG
TestNG framework inspired by JUnit/nUnit
Uses annotations
Listing 2.9: TestNG
import org.testng.annotations.Test
import org.testng.Assert.assertEquals
class AnswerTests {
@Test def verifyAnswerToLife() {
val answer = 42
assertEquals(answer, 42)
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
TestNG + Scala
ScalaTest defines trait TestNGSuite
TestNGWrapperSuite enables TestNG to test Scala while the
tests are written in Java
Listing 2.10: TestNG + Scala
import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test
class AnswerSuite extends TestNGSuite {
@Test def verifyAnswerToLife() {
val answer = 42
expect(42) { answer }
assert(answer === 42)
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
Listing 2.11: TestNG + Scala
import org.scalatest.Spec
class AnswerSpec extends Spec {
"The Answer" -- {
"should be an integer" - {
val answer = 42
assert(answer.isInstanceOf[Int] == true)
}
"should be 42" - {
val answer = 42
assert(answer === 42)
}
}
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Unit testing
Tests as specification
Behavior-driven-development (BDD) lays emphasis on
human-readable testing
ScalaTest includes a trait Spec
Spec contains describers and specifiers
Each specifier will be run as a seperate ScalaTest
Output (new AnswerSpec).execute()
The answer
- should be an integer
- should be 42
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Case Classes and Pattern Matching
Tests as specification
Case Classes
Pattern Matching
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Case classes
Listing 3.1: Definitions
abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
case class UnOp(operator: String, arg: Expr) extends Expr
case class BinOp(operator: String,left: Expr, right:
Expr) extends Expr
The case modifier
Example from the book
The case modifier adds syntactic conveniences
Factory method: val v = Val(”x”)
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Case classes
Listing 3.2: Nesting
val op = BinOp("+", Number(42), Var("x"))
The case modifier
Allows to ommit the new
All constructor parameters implicitly get a val prefix
toString, equals, hashCode are automagically added
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Listing 3.3: Matching
def simplifyTop(expr: Expr): Expr = expr match {
case UnOp("-",UnOp("-",e)) => e // Double negation
case BinOp("+", e, Number(0)) => e // Adding zero
case BinOp("*", e, Number(1)) => e // Multiplying by one
case _ => expr
}
Matching
Similar to Javas switch
Syntax: selector match {alternatives}
match is an expression and results in a value
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Patterns
e, variable pattern
, wildcard pattern, drops the value
UnOp(“-“, e), constructor pattern, matches values of type
UnOp with - as first param.
If no pattern matches a MatchError is thrown.
Listing 3.4: Matching
expr match {
case BinOp(_, _, _) => println(expr +"is a binary
operation")
case _ => println("It’s something else")
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Constant patterns
Any literal (String, Boolean, Integer) can be used as a pattern
Literals match themselves
Listing 3.5: Constant patterns
def inspect(x: Any) = x match {
case 12 => "twelve"
case Nil => "nothing"
case false => "negative"
case _ => "Cant’t say"
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Variable patterns
Variable pattern matches like a wildcard, but the result gets
assigned to the variable
Listing 3.6: Variable patterns
expr match {
case 12 => "Not right"
case number => "This is a number " + number
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
How does Scala tell them apart?
Scala uses a lexical rule to differ between variable and
constant patterns
Lowercase identifiers are treated as variable
Uppercase identifiers are assumed to be a constant
Exception to the rule?
true and false
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Constructor patterns
Check against a constructor and it’s parameters
Object gets inspected
Contents of the object are checked aswell
Listing 3.7: Constructor pattern
expr match {
case BinOp("-", m, Number(12)) => println(m + " - 12")
case _ =>
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Sequence patterns
Match against sequence types like List, Array, ...
Allows to specify the number of elements in the pattern
⇔ Wildcard, * ⇔ 0..n elements
Listing 3.8: Sequence pattern with fixed length
expr match {
case List(n, _, _) => println("Top-Element: " + n)
case List(n, _*) => println("Top-Element: " + n)
case _ =>
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Tuple patterns
A pattern like (x, y) matches a 2-tuple
Listing 3.9: Tuple pattern
expr match {
case (x, y) => println("Matched a 2-tuple")
case _ =>
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Typed patterns
Alternative for type checks and type casts
Type check: expr.instanceOf[String]
Type cast: expr.asInstanceOf[String]
Listing 3.10: Typed pattern
def getSize(e: Any) = e match {
case s: String => s.length
case m: Map[_,_] => m.size
case _ => -1
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Type erasure
Matching a Map with special element types doesn’t work!
Scala/Java use the erasure model for generics
No information about type arguments is maintained at
run-time
Arrays are an exception, they are handled differently
Listing 3.11: Type erasure - Matches all Maps!
expr match {
case m: Map[Int, String] => println("String-Int Map")
case _ =>
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Variable binding
Perform the normal match but bind the match to a variable
The @ sign binds a variable to a match
Listing 3.12: Binding a variable to a match
expr match {
case UnOp("abs", e @ UnOp("abs", _)) => e
case _ =>
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Pattern guards
Sometimes a match can have pre-conditions
The conditions are “guarding“ the match
Listing 3.13: Match with pre-condition
expr match {
case BinOp("/", _, y) if y == 0 => println("Nominator
can’t be 0")
case _ =>
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Pattern overlaps
Patterns are tried in the order they’re written
Changing the order of cases can change behaviour
Listing 3.14: Simple pattern overlap
expr match {
case BinOp("+", e, Number(10)) => println("BinOp + 10")
case BinOp("-", e, Number(10)) => println("BinOp - 10")
case BinOp(op, e, Number(10)) => println("BinOp "+op+"
10")
case _ => expr
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Sealed classes
When you match using patterns, how do you make sure you
matched everything?
sealed classes allow the compiler to know all possible cases
A sealed class can only have subclasses in the same file
The compiler can then generate warnings (match not
exhaustive)
Listing 3.15: Sealed class
sealed abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Sealed classes
If you leave out an option a warning is thrown: “warning:
match is not exhaustive! missing combination Type“
Listing 3.16: Match on a sealed class
expr match {
case Var(x) => "Variable " + x
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Sealed classes
Warning can be avoided by doing a wildcard match
Suppressed using an annotation (will be covered in Chapter
25)
Usefulness?
Why make the class sealed in the first place?
Listing 3.17: Match on a sealed class
(expr: @unchecked) match {
case Var(_) => "a variable"
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
The Option type
Standard type for optional values
Can be Some(x) or None
Is returned by HashMap, Map, ..
None is the Scala way of returning null
Listing 3.18: Unpacking an option
val map = Map(1 -> "a", 2 -> "b", 3 -> "c")
(map get 1) match {
case Some(s) => println(s)
case None => println("Not there")
}
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Patterns in other places
Patterns can be used almost anywhere
Listing 3.19: Other uses of patterns
val (a, b) = (1, 2)
for((num, letter) <- Map(1->"a", 2->"b"))
println("Num:"+num+" Letter:"+letter)
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Case sequences as partial functions
Case sequences can be used partial functions
Partial functions throw a RuntimeException if they are applied
against a value they don’t support
Compiler can throw warnings
Listing 3.20: Case sequence as partial function
val myMatch: Option[Int] => String = {
case Some(x) => "Value: " + x
case None => "Nothing there"
}
myMatch(Some(12))
// => Value: 12
Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching
Pattern matching
Case sequences as partial functions
By telling the compiler that you use a partial function you can
check whether it’s possible to apply a value
Listing 3.21:
val listMatch: PartialFunction[List[Int], Int] = {
case x :: y :: _ => y
}
// Can we apply the List?
listMatch.isDefinedAt(List(1,2,3))
// => true

Más contenido relacionado

La actualidad más candente

Reflection in java
Reflection in javaReflection in java
Reflection in javaupen.rockin
 
Java Reflection Explained Simply
Java Reflection Explained SimplyJava Reflection Explained Simply
Java Reflection Explained SimplyCiaran McHale
 
Java tutorials
Java tutorialsJava tutorials
Java tutorialssaryu2011
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.Tarunsingh198
 
Security Vulnerability Notice SE-2012-01-PUBLIC [Security vulnerabilities in ...
Security Vulnerability Notice SE-2012-01-PUBLIC [Security vulnerabilities in ...Security Vulnerability Notice SE-2012-01-PUBLIC [Security vulnerabilities in ...
Security Vulnerability Notice SE-2012-01-PUBLIC [Security vulnerabilities in ...Yury Chemerkin
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDDShai Yallin
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobGaruda Trainings
 
Unit5 java
Unit5 javaUnit5 java
Unit5 javamrecedu
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiersSrinivas Reddy
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionAlex Su
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10Terry Yoast
 

La actualidad más candente (20)

Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
core java for hadoop
core java for hadoopcore java for hadoop
core java for hadoop
 
Reflection in java
Reflection in javaReflection in java
Reflection in java
 
Java Reflection Explained Simply
Java Reflection Explained SimplyJava Reflection Explained Simply
Java Reflection Explained Simply
 
9 cm604.28
9 cm604.289 cm604.28
9 cm604.28
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
FunctionalInterfaces
FunctionalInterfacesFunctionalInterfaces
FunctionalInterfaces
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Security Vulnerability Notice SE-2012-01-PUBLIC [Security vulnerabilities in ...
Security Vulnerability Notice SE-2012-01-PUBLIC [Security vulnerabilities in ...Security Vulnerability Notice SE-2012-01-PUBLIC [Security vulnerabilities in ...
Security Vulnerability Notice SE-2012-01-PUBLIC [Security vulnerabilities in ...
 
How Scala promotes TDD
How Scala promotes TDDHow Scala promotes TDD
How Scala promotes TDD
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Junit
JunitJunit
Junit
 
Unit5 java
Unit5 javaUnit5 java
Unit5 java
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
 

Destacado

Scala case of case classes
Scala   case of case classesScala   case of case classes
Scala case of case classesVulcanMinds
 
Seminar Joomla 1.5 SEF-Mechanismus
Seminar Joomla 1.5 SEF-MechanismusSeminar Joomla 1.5 SEF-Mechanismus
Seminar Joomla 1.5 SEF-MechanismusFabian Becker
 
GNU Bourne Again SHell
GNU Bourne Again SHellGNU Bourne Again SHell
GNU Bourne Again SHellFabian Becker
 
Case class scala
Case class scalaCase class scala
Case class scalaMatt Hicks
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Destacado (7)

Scala case of case classes
Scala   case of case classesScala   case of case classes
Scala case of case classes
 
Ruby
RubyRuby
Ruby
 
Seminar Joomla 1.5 SEF-Mechanismus
Seminar Joomla 1.5 SEF-MechanismusSeminar Joomla 1.5 SEF-Mechanismus
Seminar Joomla 1.5 SEF-Mechanismus
 
GNU Bourne Again SHell
GNU Bourne Again SHellGNU Bourne Again SHell
GNU Bourne Again SHell
 
Case class scala
Case class scalaCase class scala
Case class scala
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar a Advanced Scala

Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptlirogal422
 
Java peresentation new soft
Java peresentation new softJava peresentation new soft
Java peresentation new softMohamed Refaat
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxPoonam60376
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .happycocoman
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptionsMavoori Soshmitha
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packagesmanish kumar
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of featuresvidyamittal
 
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scalaMichal Bigos
 
New Microsoft Word Document.doc
New Microsoft Word Document.docNew Microsoft Word Document.doc
New Microsoft Word Document.docbutest
 
Class loader basic
Class loader basicClass loader basic
Class loader basic명철 강
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMambikavenkatesh2
 

Similar a Advanced Scala (20)

Lecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.pptLecture-12 Java Packages and GUI Basics.ppt
Lecture-12 Java Packages and GUI Basics.ppt
 
Java peresentation new soft
Java peresentation new softJava peresentation new soft
Java peresentation new soft
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 
Scala presentationjune112011
Scala presentationjune112011Scala presentationjune112011
Scala presentationjune112011
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Scala test
Scala testScala test
Scala test
 
Scala test
Scala testScala test
Scala test
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of features
 
Dependency injection in scala
Dependency injection in scalaDependency injection in scala
Dependency injection in scala
 
New Microsoft Word Document.doc
New Microsoft Word Document.docNew Microsoft Word Document.doc
New Microsoft Word Document.doc
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Packages
PackagesPackages
Packages
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 

Último

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 

Último (20)

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 

Advanced Scala

  • 1. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Masterseminar Scala Fabian Becker FH Giessen-Friedberg 15. November 2010
  • 2. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Content 1 Packages & Imports 2 Assertions and Unit Testing 3 Case Classes and Pattern Matching
  • 3. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Packages Important to minimize coupling Scala code resides in Java global hierarchy of packages Per default code is in the unnamed package There are two ways to place code inside packages Listing 1.1 package database.mysql class Query
  • 4. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Listing 1.2 package database { package mysql { class Query } } Listing 1.3 package database.mysql { class Query }
  • 5. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Packages Packages in Scala truly nest In Java packages are in a hierarchy they don’t nest Naming a package in Java you always start at the root
  • 6. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Listing 1.4: Accessing packages package game { package ui { class Player } package engine { class Main { // Java: new game.ui.Player() val player = new ui.Player } } }
  • 7. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Packages Listing 1.5: More complex package ui { class Player } // In ui.scala package game { // In game.scala package ui { class Player } package engine { package ui { class Player } class Main { val player = new ui.Player val player2 = new game.ui.Player val player3 = new _root_.ui.Player } } }
  • 8. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Imports Importing packages Importing packages allows to directly access members in that package Scala imports are similar to the imports from Java Imports can appear anywhere
  • 9. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Imports Listing 1.6.1: Defining Enemies package game abstract class Enemy( val name: String, var lvl: Int ) object Enemies { object Dwarf extends Enemy("Gimli", 10) object Orc extends Enemy("Bwarg", 5) object Necromancer extends Enemy("Destructor", 99) val col = List(Dwarf, Orc, Necromancer) }
  • 10. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Imports Listing 1.6.2: Importing Enemies // Simple import for Enemy import game.Enemy // Import all members of game import game._ // Import all members of Enemies import game.Enemies._
  • 11. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Imports Listing 1.7: Import in a function def showEnemy(enemy: Enemy) { import enemy._ println("Enemy - Name:"+name+" Level:"+lvl) } Imports Imports can appear in functions They import all members of a object
  • 12. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Selective Imports Listing 1.8: Selective Imports // Only import Dwarf and Orc import game.Enemies.{Dwarf, Orc} // Renaming an import import game.Enemies.{Necromancer => Necro} // Rename one, import the rest import game.Enemies.{Orc => O, _} // Exclude an import import game.Enemies.{Orc => _, _}
  • 13. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Selective Imports Selective Imports Import: import game.Enemies.{Orc} ⇔ game.Enemies.Orc Rename: originalname ⇒ newname Hide Packages: original − name ⇒ Catch All: importgame.Enemies.{ } ⇔ game.Enemies.
  • 14. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Implicit imports Listing 1.9: Implicit imports import java.lang._ // everything in the java.lang package import scala._ // everything in the scala package import Predef._ // everything in the Predef object Implicit imports Packages imported implicitly by Scala scala. overshadows java.lang. scala.StringBuilder ⇒ java.lang.StringBuilder
  • 15. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Access modifiers Listing 1.10: Private modifiers class Outer { class Inner { private def f() { println("f") } class MostInner { f(); // OK } } (new Inner).f() // fails } Private Scala: private members are only accessible inside the class or object that contain their definition Java allows outer classes to access private members of their inner classes
  • 16. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Access modifiers Protected Java: protected members are accessible within the same package Scala: Only from the object they are defined in and their subclasses Public There is no explicit modifier for public All members not labeled private or protected are public Accessable from anywhere
  • 17. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Access modifiers Scope of protection Scala allows to control access to a level X private[X] and protected[X] allow access up to X Listing 1.11: Example package game { package ui { private[game] class Player { private[this] var hp = 42 protected[ui] def getHP() { hp } } } }
  • 18. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Assertions and Unit Testing Testing Code Assertions for run-time checking Unit testing for development testing of small units
  • 19. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Assertions Listing 2.1: Assert def add1(x: Int) : Int = { val z = x + 1 assert(z > x) return z } println(add1(41)) Assertion methods Method assert predefined in Predef assert(condition) assert(condition, explanations) If the condition doesn’t hold, an AssertionError is thrown
  • 20. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Assertions Listing 1.13: Ensuring def lvlUp(e: Enemy): Enemy = { if(e.lvl < 100) { e.lvl = e.lvl + 1 } ensuring(e.lvl < 100) e } Ensuring Method assert predefined in Predef assert(condition) assert(condition, explanations) If the condition doesn’t hold, an AssertionError is thrown
  • 21. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing ScalaTest org.scalatest.Suite (http://www.scalatest.org/) Can be executed on the Scala console: (new HitchhikerSuite).execute() Listing 2.3: Simple testcase import org.scalatest.Suite class HitchhikerSuite extends Suite { def testAnswerToLife() { val answer = 42 assert(answer == 42) } }
  • 22. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing ScalaTest with Fun ScalaTest allows different styles of testing FunSuite is a trait that overrides execute and allows to define tests as function values Naming the tests is easier (you can use Strings) Listing 2.4: FunSuite import org.scalatest.FunSuite class HitchhikerSuite extends FunSuie { test("The answer to life should be 42") { val answer = 42 assert(answer == 42) } }
  • 23. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing ScalaTest - Informative testing A normal assertion produces long error messages but do not show why the match failed ScalaTest defines the === operator for assert(), shows the values Alternative: expect(value) { block } Listing 2.5: Informative testing assert(answer === 42) // or expect(42) { answer }
  • 24. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing ScalaTest - Catching Exceptions Testing for Exceptions can be done with intercept() Listing 2.6: Intercept s = "hi" intercept[IndexOutOfBoundsException] { s.charAt(-1) }
  • 25. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing JUnit Most popular test framework for Java Can easily be used from Scala Does not natively support Scala’s assertion syntax Listing 2.7: JUnit Testcase import junit.framework.TestCase import junit.framework.Assert.assertEquals import junit.framework.Assert.fail class MyTestCase extends TestCase { def testAnswer() { val answer = 42 assertEquals(42, answer) } }
  • 26. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing JUnit + Scala ScalaTest defines JUnit4Suite (requires JUnit 4 of course) JUnit4Suite extends TestCase Listing 2.8: Using Scala syntax in JUnit import org.scalatest.junit.JUnit4Suite class AnswerSuite extends JUnit4Suite { def testAnswerToLife() { var answer = 42 assert(answer === 42) } }
  • 27. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing TestNG TestNG framework inspired by JUnit/nUnit Uses annotations Listing 2.9: TestNG import org.testng.annotations.Test import org.testng.Assert.assertEquals class AnswerTests { @Test def verifyAnswerToLife() { val answer = 42 assertEquals(answer, 42) } }
  • 28. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing TestNG + Scala ScalaTest defines trait TestNGSuite TestNGWrapperSuite enables TestNG to test Scala while the tests are written in Java Listing 2.10: TestNG + Scala import org.scalatest.testng.TestNGSuite import org.testng.annotations.Test class AnswerSuite extends TestNGSuite { @Test def verifyAnswerToLife() { val answer = 42 expect(42) { answer } assert(answer === 42) } }
  • 29. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing Listing 2.11: TestNG + Scala import org.scalatest.Spec class AnswerSpec extends Spec { "The Answer" -- { "should be an integer" - { val answer = 42 assert(answer.isInstanceOf[Int] == true) } "should be 42" - { val answer = 42 assert(answer === 42) } } }
  • 30. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Unit testing Tests as specification Behavior-driven-development (BDD) lays emphasis on human-readable testing ScalaTest includes a trait Spec Spec contains describers and specifiers Each specifier will be run as a seperate ScalaTest Output (new AnswerSpec).execute() The answer - should be an integer - should be 42
  • 31. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Case Classes and Pattern Matching Tests as specification Case Classes Pattern Matching
  • 32. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Case classes Listing 3.1: Definitions abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr case class UnOp(operator: String, arg: Expr) extends Expr case class BinOp(operator: String,left: Expr, right: Expr) extends Expr The case modifier Example from the book The case modifier adds syntactic conveniences Factory method: val v = Val(”x”)
  • 33. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Case classes Listing 3.2: Nesting val op = BinOp("+", Number(42), Var("x")) The case modifier Allows to ommit the new All constructor parameters implicitly get a val prefix toString, equals, hashCode are automagically added
  • 34. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Listing 3.3: Matching def simplifyTop(expr: Expr): Expr = expr match { case UnOp("-",UnOp("-",e)) => e // Double negation case BinOp("+", e, Number(0)) => e // Adding zero case BinOp("*", e, Number(1)) => e // Multiplying by one case _ => expr } Matching Similar to Javas switch Syntax: selector match {alternatives} match is an expression and results in a value
  • 35. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Patterns e, variable pattern , wildcard pattern, drops the value UnOp(“-“, e), constructor pattern, matches values of type UnOp with - as first param. If no pattern matches a MatchError is thrown. Listing 3.4: Matching expr match { case BinOp(_, _, _) => println(expr +"is a binary operation") case _ => println("It’s something else") }
  • 36. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Constant patterns Any literal (String, Boolean, Integer) can be used as a pattern Literals match themselves Listing 3.5: Constant patterns def inspect(x: Any) = x match { case 12 => "twelve" case Nil => "nothing" case false => "negative" case _ => "Cant’t say" }
  • 37. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Variable patterns Variable pattern matches like a wildcard, but the result gets assigned to the variable Listing 3.6: Variable patterns expr match { case 12 => "Not right" case number => "This is a number " + number }
  • 38. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching How does Scala tell them apart? Scala uses a lexical rule to differ between variable and constant patterns Lowercase identifiers are treated as variable Uppercase identifiers are assumed to be a constant Exception to the rule? true and false
  • 39. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Constructor patterns Check against a constructor and it’s parameters Object gets inspected Contents of the object are checked aswell Listing 3.7: Constructor pattern expr match { case BinOp("-", m, Number(12)) => println(m + " - 12") case _ => }
  • 40. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Sequence patterns Match against sequence types like List, Array, ... Allows to specify the number of elements in the pattern ⇔ Wildcard, * ⇔ 0..n elements Listing 3.8: Sequence pattern with fixed length expr match { case List(n, _, _) => println("Top-Element: " + n) case List(n, _*) => println("Top-Element: " + n) case _ => }
  • 41. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Tuple patterns A pattern like (x, y) matches a 2-tuple Listing 3.9: Tuple pattern expr match { case (x, y) => println("Matched a 2-tuple") case _ => }
  • 42. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Typed patterns Alternative for type checks and type casts Type check: expr.instanceOf[String] Type cast: expr.asInstanceOf[String] Listing 3.10: Typed pattern def getSize(e: Any) = e match { case s: String => s.length case m: Map[_,_] => m.size case _ => -1 }
  • 43. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Type erasure Matching a Map with special element types doesn’t work! Scala/Java use the erasure model for generics No information about type arguments is maintained at run-time Arrays are an exception, they are handled differently Listing 3.11: Type erasure - Matches all Maps! expr match { case m: Map[Int, String] => println("String-Int Map") case _ => }
  • 44. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Variable binding Perform the normal match but bind the match to a variable The @ sign binds a variable to a match Listing 3.12: Binding a variable to a match expr match { case UnOp("abs", e @ UnOp("abs", _)) => e case _ => }
  • 45. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Pattern guards Sometimes a match can have pre-conditions The conditions are “guarding“ the match Listing 3.13: Match with pre-condition expr match { case BinOp("/", _, y) if y == 0 => println("Nominator can’t be 0") case _ => }
  • 46. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Pattern overlaps Patterns are tried in the order they’re written Changing the order of cases can change behaviour Listing 3.14: Simple pattern overlap expr match { case BinOp("+", e, Number(10)) => println("BinOp + 10") case BinOp("-", e, Number(10)) => println("BinOp - 10") case BinOp(op, e, Number(10)) => println("BinOp "+op+" 10") case _ => expr }
  • 47. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Sealed classes When you match using patterns, how do you make sure you matched everything? sealed classes allow the compiler to know all possible cases A sealed class can only have subclasses in the same file The compiler can then generate warnings (match not exhaustive) Listing 3.15: Sealed class sealed abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr
  • 48. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Sealed classes If you leave out an option a warning is thrown: “warning: match is not exhaustive! missing combination Type“ Listing 3.16: Match on a sealed class expr match { case Var(x) => "Variable " + x }
  • 49. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Sealed classes Warning can be avoided by doing a wildcard match Suppressed using an annotation (will be covered in Chapter 25) Usefulness? Why make the class sealed in the first place? Listing 3.17: Match on a sealed class (expr: @unchecked) match { case Var(_) => "a variable" }
  • 50. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching The Option type Standard type for optional values Can be Some(x) or None Is returned by HashMap, Map, .. None is the Scala way of returning null Listing 3.18: Unpacking an option val map = Map(1 -> "a", 2 -> "b", 3 -> "c") (map get 1) match { case Some(s) => println(s) case None => println("Not there") }
  • 51. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Patterns in other places Patterns can be used almost anywhere Listing 3.19: Other uses of patterns val (a, b) = (1, 2) for((num, letter) <- Map(1->"a", 2->"b")) println("Num:"+num+" Letter:"+letter)
  • 52. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Case sequences as partial functions Case sequences can be used partial functions Partial functions throw a RuntimeException if they are applied against a value they don’t support Compiler can throw warnings Listing 3.20: Case sequence as partial function val myMatch: Option[Int] => String = { case Some(x) => "Value: " + x case None => "Nothing there" } myMatch(Some(12)) // => Value: 12
  • 53. Packages & Imports Assertions and Unit Testing Case Classes and Pattern Matching Pattern matching Case sequences as partial functions By telling the compiler that you use a partial function you can check whether it’s possible to apply a value Listing 3.21: val listMatch: PartialFunction[List[Int], Int] = { case x :: y :: _ => y } // Can we apply the List? listMatch.isDefinedAt(List(1,2,3)) // => true