Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Test {
- trait MyType
- trait Ext extends MyType
- case class Non() extends MyType
- def caster[A <: MyType](a: MyType) = {
- a.asInstanceOf[A] // Exception should happen here, but doesn't
- println("This should never print")
- }
- def run() {
- val b = caster[Ext](Non())
- println("No exception yet.")
- val c = Non().asInstanceOf[Ext] // Exception is thrown if we do it directly here, which imo. should be exactly what we do above just via the method.
- println(b)
- }
- }
- /*
- scala> Test.run
- This should never print
- No exception yet.
- java.lang.ClassCastException: Test$Non cannot be cast to Test$Ext
- at Test$.run(<console>:53)
- at .<init>(<console>:40)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement