class CreditRiskIndicators (val ead: Double) {} abstract class FinancialInstrument { def getCreditRiskIndicators() : CreditRiskIndicators } abstract class Account { def getCreditRiskIndicators() : CreditRiskIndicators } abstract class Position { def getCreditRiskIndicators() : CreditRiskIndicators }
Friday, September 28, 2012
Structural Typing Zur Hilfe
Structural Food Typing
abstract class Dickmacher {} case class Beverage (name: String, cal: Int) extends Dickmacher case class Food (name: String, cal: Int) extends Dickmacher case class Meal (name: String, food: Food, drink: Beverage) extends Dickmacher case class Combo (starter: Dickmacher, main: Dickmacher) extends Dickmacher //hat wie Food einen val 'cal', erbt aber nicht von Dickmacher class Elephant(val cal: Int) { override def toString() : String = "Elephant" } class Person(name: String) { def eat(food: Food) = { println("Ich habe " + food + " mit " + food.cal + " cal gegessen.") } } val person = new Person("Horst") person.eat(Food("Diatschokolade", 5)) // person.eat(new Elephant(500000)) compile error class Allesfresser(name: String) { def eat(food: { def cal : Int}) = { println("Ich habe " + food + " mit " + food.cal + " cal gegessen.") } } val allesFresser = new Allesfresser("Anna") allesFresser.eat(Food("Schnitzel", 400)) allesFresser.eat(new Elephant(500000))
Pattern Matching Unpack
abstract class Dickmacher case class Beverage (name: String, cal: Int) extends Dickmacher case class Food (name: String, cal: Int) extends Dickmacher case class Meal (name: String, food: Food, drink: Beverage) extends Dickmacher case class Combo (starter: Dickmacher, main: Dickmacher) extends Dickmacher def unpack(food: Dickmacher) : List[Dickmacher] = food match { case a: Beverage => a :: Nil case b: Food => b :: Nil case Meal(_, b , a) => a :: b :: Nil case Combo(a, b) => unpack(a) ::: unpack(b) case _ => Nil } val coke = Beverage("Cola Light", 50) val burger = Food("Big Mac", 400) val cokeLight = Beverage("Cola", 100) val wannaBeBurger = Food("Veggie Mac", 50) val cokeCombo = Combo(coke, cokeLight) println(unpack(cokeCombo)) val burgerCombo = Combo(burger, wannaBeBurger) println(unpack(burgerCombo)) val monsterCombo = Combo(Combo(burgerCombo, cokeCombo), Food("Nachspeise", 600)); println(unpack(monsterCombo))
Pattern Matching Simplify
abstract class Dickmacher case class Beverage (name: String, cal: Int) extends Dickmacher case class Food (name: String, cal: Int) extends Dickmacher case class Meal (name: String, food: Food, drink: Beverage) extends Dickmacher case class Combo (starter: Dickmacher, main: Dickmacher) extends Dickmacher def simplify(d: Dickmacher) : Dickmacher = { d match { case Combo(Food(x,cal1), Food(y,cal2)) => simplify(Food(x + " mit " + y, cal1 + cal2)) case Combo(Beverage(x,cal1), Beverage(y,cal2)) => simplify(Beverage(x + " mit " + y, cal1 + cal2)) case Combo(a, b) => Combo(simplify(a), simplify(b)) case _ => d } } val coke = Beverage("Cola Light", 50) val burger = Food("Big Mac", 400) val cokeLight = Beverage("Cola", 100) val wannaBeBurger = Food("Veggie Mac", 50) val cokeCombo = Combo(coke, cokeLight) println(simplify(cokeCombo)) val burgerCombo = Combo(burger, wannaBeBurger) println(simplify(burgerCombo)) val monsterCombo = Combo(burgerCombo, cokeCombo); println(simplify(monsterCombo))
Subscribe to:
Posts (Atom)