ScalaでBoost.Interfaces的なこと。

Structual Subtypingで、継承関係がないdrawというメソッドを持っているクラスをなんでも入れられるリスト。

class Rectangle { def draw = println("Rectangle")  }
class Circle    { def draw = println("Circle") }

object Main {
  def main(args: Array[String]) {
    type Drawable = { def draw: Unit }

    val ls = List[Drawable](new Rectangle, new Circle)
    ls foreach(_.draw)
  }
}
Rectangle
Circle