Saturday, November 20, 2010

Typecasting in Scala

In Java :

Car car = (Car)Vehicle.factory(Car.class);

(Of course, the above is a really bad example. But I can't think of a better one right now)

In Scala :

var car = Vehicle.factory(classOf[String]).asInstanceOf[Car];

So, there are two core class functions in Scala: classOf[C] and asInstanceOf[C]

No comments:

Post a Comment