advanced scala

Download Advanced Scala

Post on 10-Nov-2014

988 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

This presentation was part of a seminar in a masters course. It covers 3 chapters of Martin Odersky's Book "Programming in Scala" (Packages and Imports, Assertions and Unit testing, Case Classes and Pattern Matching).

TRANSCRIPT

  • 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 dont 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: Dening 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 new name 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 modiers Listing 1.10: Private modiers 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 denition 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 modiers Protected Java: protected members are accessible within the same package Scala: Only from the object they are dened in and their subclasses Public There is no explicit modier 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 modiers 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 predened in Predef assert(condition) assert(condition, explanations) If the condition doesnt 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 predened in Predef assert(condition) assert(condition, explanations) If the condition doesnt hold, an AssertionError is thrown
  • 21.