Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:06:40+00:00 2026-06-08T15:06:40+00:00

How do I cast this to an Int and not Some(Int) val a: Option[Any]

  • 0

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]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:06:42+00:00Added an answer on June 8, 2026 at 3:06 pm

    You could do a.get.asInstanceOf[Int] however it is unsafe. A better way would be to retain the type information i.e. using a Option[Int] instead of an Option[Any]. Then you would not need to cast the result with asInstanceOf.

    val a:Option[Int] = Some(1)
    val i = a.get
    

    Using get directly is unsafe since if the Option is a None an exception is thrown. So using getOrElse is safer. Or you could use pattern matching on a to get the value.

    val a:Option[Any] = Some(1) // Note using Any here
    val i = (a match {
      case Some(x:Int) => x // this extracts the value in a as an Int
      case _ => Int.MinValue
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this: (new[]{1,2,3}).Cast<decimal>(); result in an InvalidCastException: Specified cast is not valid.
This is not allowed in Mysql: SELECT CAST(0 as DOUBLE) as ZERO How do
I realize this error is usually due to some syntax or type issues but
So I have some code that looks like this: int a[10]; a = arrayGen(a,9);
I have a table that looks like this: CREATE TABLE [dbo].[StudentTime] ([StudentTimeID] [int] NOT
I give up, how do I cast this? class AmountIsTooHighConverter : IValueConverter { public
I'm getting a warning: Return makes pointer from integer without a cast for this
In Scala, how can I do something like this: def cast [Type] (x: _
CAST(IH.InvoiceDate AS date) AS InvoiceDate this returns 2012-03-16 is it possible to format this
Trying to do something like this: select 'Setup for Car ' + CAST(varchar(50), @CarID)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.