JohanSJA
JohanSJA

Reputation: 705

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?

Upvotes: 3

Views: 180

Answers (2)

Arjan Blokzijl
Arjan Blokzijl

Reputation: 6888

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

Upvotes: 3

user382157
user382157

Reputation:

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
}

Upvotes: 6

Related Questions