Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Types

I was trying to convert an object with Object type into FontUIResource type. In Java, it would be

FontUIResource font = (FontUIResource)value

How would I do that in Scala?

like image 423
JohanSJA Avatar asked Sep 03 '26 18:09

JohanSJA


2 Answers

You can say value.asInstanceOf[FontUIResource], or you can use a match-case block:

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

You mean casting, not Boxing and Unboxing, since that applies to primitive values. value.asInstanceOf[FountUIResource] is the way to cast this in Scala.

like image 21
Arjan Blokzijl Avatar answered Sep 05 '26 09:09

Arjan Blokzijl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!