How do I cast this to an Int and not Some(Int)
val a: Option[Any] = Some(1)
I tried toInt and it gave an error value toInt is not a member of Option[Any]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could do
a.get.asInstanceOf[Int]however it is unsafe. A better way would be to retain the type information i.e. using aOption[Int]instead of anOption[Any]. Then you would not need to cast the result withasInstanceOf.Using
getdirectly is unsafe since if theOptionis aNonean exception is thrown. So usinggetOrElseis safer. Or you could use pattern matching onato get the value.