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

Touki na Confiturze 2013 – ciąg dalszy

Kolejna partia nagrań z konferencji została udostępniona. Wśród nich m.in. prezentacja Michała Trzaskowskiego o rozpoznawaniu oczekiwań klienta podczas prowadzenia projektu z wykorzystaniem ”zwinnych” metodyk projektowych oraz wystąpienie Macieja Próchniaka o możliwościach Slick, jednej z bibliotek w Scali.

Read emails from imap with Spring Intergration

What's the easiest way to read emails from IMAP account in Java? Depends what your background is. If you have any experience in Apache Camel, ServiceMix, Mule, you already know the answer. If you don't, and your application is using Spring alr...What's the easiest way to read emails from IMAP account in Java? Depends what your background is. If you have any experience in Apache Camel, ServiceMix, Mule, you already know the answer. If you don't, and your application is using Spring alr...

TouK na targach pracyTouK at the job fair

Zapraszamy na XI Targi Pracy i Praktyk dla Elektroników i Informatyków. Odwiedź nasze stoisko w dniach 4-5 marca w godz. 9:30-15:30. Politechnika Warszawska Pierwsze piętro budynku Wydziału Elektroniki i Techniki Informacyjnych Nowowiejska 15/19We invite you to 11th Job and Internship Fair for Electronic Engineers and IT Specialists. Come and visit our stand between 4-5 March 9:30 am and 15 :30 pm Warsaw University of Technology the first floor of the Electronics faculty building Nowowiejska 15/19