Aladdin - Scala Bugtracking
[#789] project: compiler priority: low category: missing feature
submitter assigned to status date submitted
Burak Martin fixed 2006-10-24 18:40:52.0
subject more accurate type tests
code
object main { // don't do this at home

  trait Impl

  trait SizeImpl extends Impl { def size = 42 }

  trait ColorImpl extends Impl { def color = "red" }

  type Both = SizeImpl with ColorImpl

  def info(x:Impl) = x match {
    case x:Both      => "size  "+x.size+" color "+x.color // you wish
    case x:SizeImpl  => "size  "+x.size
    case x:ColorImpl => "color "+x.color
    case _           => "n.a."
  }

  def main(args:Array[String]): Unit = {
    // make up some class that has a size
    class MyNode extends SizeImpl
    Console.println("hello " + info(new MyNode))
  }
}
what happened
deprecation warning, and runtime classcast exception
what expected couldn't we rewrite a type test isInstanceOf[Both] into isInstanceOf[SizeImpl] && isInstanceOf[ColorImpl]?
[back to overview]
Changes of this bug report
Martin  edited on  2006-10-26 11:10:44.0
We could, but then you also need to put the right asInstanceOf's. I.e.
if (x.isInstanceOf[SizeImpl] && x.isInstanceOf[ColorImpl])
  x$0 = x.asInstanceOf[SizeImpl]
  x$1 = x.asInstanceOf[ColorImpl]
  "size  "+x$0.size+" color "+x$1.color
It's feasible. It might be worth the added complexity, but I am not sure yet.
Burak  edited on  2006-10-26 15:12:54.0
Yes, but that is already done somehwere! Try -print:erasure on the example, the asInstanceOfs are correct. It's only the instanceOf that needs action, unless I am missing some interactions.
Martin  edited on  2006-11-01 16:45:24.0
Now it typechecks, and produces a classcast exception. We still need to add the double instanceOf test. Reassigned to Burak.
Martin  edited on  2006-11-01 17:25:08.0
After discussing with Burak we decided that the changes are best made in Erasure. So I took care of it.