Last active
March 6, 2018 21:55
-
-
Save tjheslin1/8baae8b4e8bae2882b81e0dbf821f866 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait T | |
// defined trait T | |
case class A() extends T | |
// defined class A | |
case class B(a: A) extends T | |
// defined class B | |
def check[Y <: T](t: T): Boolean = t.isInstanceOf[Y] | |
// defined function check | |
val a = A() | |
// a: A = A() | |
val b = B(a) | |
// b: B = B(A()) | |
check[T](a) | |
check[T](b) | |
check[A](a) | |
check[B](b) | |
// res: Boolean = true <- same result for all above | |
// *********** | |
check[A](b) | |
// res: Boolean = true | |
// *** `b` is not an instance of A | |
b.isInstanceOf[A] | |
// res: Boolean = false | |
// *********** | |
// *********** | |
check[B](a) | |
// res: Boolean = true | |
// *** `a` is not an instance of B | |
// a.isInstanceOf[B] | |
// res: Boolean = false | |
// *********** | |
def checkA(t: T): Boolean = t.isInstanceOf[A] | |
// defined function checkA | |
checkA(a) | |
// res: Boolean = true | |
checkA(b) | |
// res: Boolean = false | |
// *** same for B, `checkB()` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment