Type conversion

I tried to convert an object with an object type to a FontUIResource type. In Java, it will be

FontUIResource font = (FontUIResource)value

How do I do this in Scala?

+3
source share
2 answers

You can say value.asInstanceOf[FontUIResource], or you can use the matching block:

value match{
  case f:FontUIResource => 
    //do something with f, which is safely cast as a FontUIResource
  case _ => 
    //handle the case when it not the desired type
}
+6
source

You mean casting, not boxing and Unboxing, as this applies to primitive values. value.asInstanceOf[FountUIResource]is a way to do it in Scala.

+3
source

Source: https://habr.com/ru/post/1764099/


All Articles