Skip to content

Instantly share code, notes, and snippets.

@DarkDimius
Last active April 19, 2017 08:53
Show Gist options
  • Select an option

  • Save DarkDimius/6c5a341f324a63fa7077 to your computer and use it in GitHub Desktop.

Select an option

Save DarkDimius/6c5a341f324a63fa7077 to your computer and use it in GitHub Desktop.
Reachability examples
trait A {/* has a `def toString: String` inherited from scala.Any */}
class B extends A { override def toString = "B"}
class C extends A { override def toString = "C" /* is this dead code? */ }
def main(a: Array[String]): Unit = {
def foo(a: A) = a.toString // does it call .toString on all the A’s?
val b: B = new B;
val c: C = new C;
foo(b)
}
trait A {/* has a `def toString: String` inherited from scala.Any */}
class B extends A { override def toString = "B" /* is this dead code? */}
class C extends B { override def toString = "C"}
def main(a: Array[String]): Unit = {
def foo(a: A) = a.toString // does it call .toString on all the A’s?
val b: B = new B;
val c: C = new C;
foo(c)
}
trait A {/* has a `def toString: String` inherited from scala.Any */}
trait B extends A { override def toString = super.toString + "B" /* is this dead code? */}
trait C extends A { override def toString = super.toString + "C" /* is this dead code? */}
def main(a: Array[String]): Unit = {
def foo(a: A) = a.toString // does it call .toString on all the A’s?
val b: B = new B with C{};
foo(b)
}
trait A {/* has a `def toString: String` inherited from scala.Any */}
trait B extends A { override def toString = super[A].toString + "B" /* is this dead code? */} // triggers https://issues.scala-lang.org/browse/SI-9645
trait C extends A { override def toString = super.toString + "C" /* is this dead code? */}
def main(a: Array[String]): Unit = {
def foo(a: A) = a.toString // does it call .toString on all the A’s?
val b: B = new B with C{};
foo(b)
}
trait A { type I; val s: I; def toString = s.toString}
class B extends A { type I = Int; val s = 0;}
class E { override def toString = ??? /* is this dead code? */}
def main(a: Array[String]): Unit = {
def foo(a: A) = a.toString // does it call .toString on all the A’s?
val b: B = new B;
val e: E = new E;
foo(b)
}
trait A {/* has a `def toString: String` inherited from scala.Any */}
class B {self => class Inner extends A {override def toString = self.toString}}
class C extends B{ override def toString = "B" /* is this dead code? */ }
def main(a: Array[String]): Unit = {
def foo(a: A) = a.toString // does it call .toString on all the A’s?
val c: B = new C;
val inner = new (c.Inner)
foo(inner)
}
trait A { def toStringUnapplied: () => String}
class B {self => class Inner extends A {def toStringUnapplied = self.toString _ }}
class C extends B { override def toString = "B" /* is this dead code? */ }
def main(a: Array[String]): Unit = {
def foo(a: A) = a.toStringUnapplied
val c: B = new C;
val inner = new (c.Inner)
foo(inner)
}
class A { def toString = "A"}
class B { def toString = "B"}
def main(a: Array[String]): Unit = {
def foo[A](a: A) = a.toString
val a: A = new A;
val b: B = new B;
foo(a)
}
trait A
class B {self => class Inner extends A { override def toString = self.toString}}
class C[T](val f: T) extends B { override def toString = f.toString}
class E {override def toString = "E" /* is this dead code? */ }
def main(a: Array[String]): Unit = {
def foo(a: A) = a.toString // does it call .toString on all the A’s?
val e: E = new E
val c: B = new C(1);
val inner = new (c.Inner)
foo(inner)
}
@developandbehappy

Copy link
Copy Markdown

Why Go compiler quickly and Scala so slow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment