fork download
  1. def is[T <: Exp: Manifest] = {
  2. implicitly[Manifest[T]].erasure.isInstance(this)
  3. }
  4. def as[T <: Exp] = asInstanceOf[T]
  5. }
  6.  
  7. trait UnaryExp extends Exp { def arg: Int }
  8. trait BinaryExp extends Exp { def left: Int; def right: Int }
  9. case class Plus(left: Int, right: Int) extends BinaryExp
  10.  
  11. object Main extends App {
  12. val b = Plus(1, 2)
  13.  
  14. if (b.is[BinaryExp]) {
  15. println(b.as[BinaryExp].left + b.as[BinaryExp].right)
  16. }
  17.  
  18. if (!b.is[UnaryExp]) {
  19. println(b + " is not unary expression")
  20. }
  21.  
  22. }
  23.  
Success #stdin #stdout 0.37s 382080KB
stdin
Standard input is empty
stdout
3
Plus(1,2) is not unary expression