Use asInstanceOf[T] carefully!

BackgroundScala has nice static type checking engine but from time to time there are situations when we must downcast some general object. If this casting is not possible we expect that virtual machine will throw ClassCastExeption as fast as possible. …

Background

Scala has nice static type checking engine but from time to time there are situations when we must downcast some general object. If this casting is not possible we expect that virtual machine will throw ClassCastExeption as fast as possible. Although it is not always true. Consider code below.

Suprisingly when we run this test we will see:

Solution

Why this happens? Because type T is erasured during compile. The problem is that compiler doesn’t warn about it. Method asInstanceOf[T] is treated as any other regular generic method. If we want to be noticed about type erasure we should use pattern matching:

And then during compilation we will see:

But how to fix this? We can provide implicit evidence parameter:

But we will still have no error if we cast value to generic type e.g.:

With help comes shapeless with Type safe cast. Using this approach casting will be available in compile time only when exists evidence how it is possible.

Summary

Summarizing:
* Use pattern matching instead of asInstanceOf[T]
* If you are using asInstanceOf[T] make sure that target type is not erasured
* Use ClassTag implicit evidence parameter if you are casting only to not generic types
* Use shapeless Typeable in all other situations

# CodeCode with tests is available on

[GitHub][2]
You May Also Like

4Developers 2010 Review

I've been to 4Developers in 2009 in Cracow, together with Tomasz Przybysz and we had very nice impressions, no wonder then I wanted to signed up for 2010 edition in Poznań as well. Tomasz was sick, but Jakub Kurlenda decided to come with me. This time...I've been to 4Developers in 2009 in Cracow, together with Tomasz Przybysz and we had very nice impressions, no wonder then I wanted to signed up for 2010 edition in Poznań as well. Tomasz was sick, but Jakub Kurlenda decided to come with me. This time...

GWT Designer for Eclipse 3.6 can cause project compile freeze

Lately I installed GWT Designer for Eclipse Helios (3.6). I wanted to check it's features. They aren't so cool I've expected but that's other story. The problem was that suddenly my main GWT enabled project began to freeze during compilation.  The...Lately I installed GWT Designer for Eclipse Helios (3.6). I wanted to check it's features. They aren't so cool I've expected but that's other story. The problem was that suddenly my main GWT enabled project began to freeze during compilation.  The...