Aladdin - Scala Bugtracking
[#450] project: compiler priority: low category: feature
submitter assigned to status date submitted
Nikolay Martin won't fix 2005-06-29 10:28:15.0
subject [contrib #61] Lax typechecking of dependant types
code
object pt {
    abstract class Point {
	type This <: Point;
	def x : int;
	def eq(p: This): Boolean = (x == p.x);
    }
}
object cpt {
    abstract class ColorPoint extends pt. Point {
	type This <: ColorPoint;
	def col: String;
	override def eq(p: This): Boolean = (x == p.x) && (col == p.col);
    }
}
object main {
    val c = new pt.Point { type This = pt.Point; def x = 0; };
    val d = new cpt.ColorPoint { type This = cpt.ColorPoint; def x = 1; 
	def col = "blue"; };
    val b = d.eq(c);
}
what happened
the code compiled
what expected a type error: d.eq(c) should be an error
[back to overview]
Changes of this bug report
Nikolay  edited on  2005-06-29 10:44:31.0
Transferred from the bug contributions.
Nikolay  edited on  2005-06-29 10:56:44.0
The problem seems to be the name of the 'eq' method. If I change it to 'equ' the compiler catches the error. Maybe the AnyRef.eq method interferes with typechecking. In Definitions it is created as 'final' but this doesn't prevent from overloading it in subclasses, like this example shows:
class Foo {
  def eq(f: Foo): Boolean = true;
}

object Test {
  def main(args: Array[String]): Unit = {
    val f1, f2 = new Foo();
    System.out.println(f1 eq f2);
    System.out.println(f1 eq (f2: AnyRef));
  }
}
Maybe we should forbid defining methods with names 'eq' or 'ne' and hardcode it in the compiler.
Martin  edited on  2005-06-29 17:29:56.0
The problem is indeed the `eq' method which takes AnyRef parameter. Your `eq' definition in class Foo just created a new overloaded version for it. This version does not match, but the original, inherited version does. Hence, all this is a consequence of the static overloading scheme, not an error.