Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.90 KB | None | 0 0
  1. // ---------Framework:---------
  2.  
  3. abstract class Engine {
  4.     abstract type MixIn
  5.     class Base(val children: Seq[Base]) extends MixIn
  6.     abstract def doSomething(base: Base)
  7.    
  8.     final class Spec1(val foo: String, children: Seq[Base]) extends Base(children)
  9.     final class Spec2(val bar: String, children: Seq[Base]) extends Base(children)
  10. }
  11.  
  12. object Engine {
  13.     def runExample(engine: Engine) {
  14.         val spec1 = new engine.Spec1("foo", Seq.empty)
  15.         engine.doSomething(spec1)
  16.         val spec2 = new engine.Spec2("foo", Seq(new Spec1("foo", Seq.empty)))
  17.         engine.doSomething(spec2)
  18.     }
  19. }
  20.  
  21. // ---------Client:---------
  22.  
  23. class ConcreteEngine {
  24.     trait Height {
  25.         val height = if(children.isEmpty()) 0 else children.map{_.height}.max
  26.     }
  27.     override type MixIn = Height
  28.    
  29.     override def doSomething(base: Base) {
  30.         println("Node @ " + base.height)
  31.     }
  32. }
  33.  
  34. object Main extends App {
  35.     Engine.run(new ConcreteEngine())
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement