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 7535461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:18:24+00:00 2026-05-30T06:18:24+00:00

Im using scala Map#get function, and for every accurate query it returns as Some[String]

  • 0

Im using scala Map#get function, and for every accurate query it returns as Some[String]

IS there an easy way to remove the Some?

Example:

def searchDefs{
    print("What Word would you like defined? ")
    val selection = readLine
    println(selection + ":\n\t" + definitionMap.get(selection))
  }

When I use this method and use the following Input:

What Word would you like defined? Ontology

The returned Value is:

Ontology:
    Some(A set of representational primitives with which to model a domain of knowledge or discourse.)

I would like to remove the Some() around that.

Any tips?

  • 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-05-30T06:18:25+00:00Added an answer on May 30, 2026 at 6:18 am

    There are a lot of ways to deal with the Option type. First of all, however, do realize how much better it is to have this instead of a potential null reference! Don’t try to get rid of it simply because you are used to how Java works.

    As someone else recently stated: stick with it for a few weeks and you will moan each time you have to get back to a language which doesn’t offer Option types.

    Now as for your question, the simplest and riskiest way is this:

    mymap.get(something).get
    

    Calling .get on a Some object retrieves the object inside. It does, however, give you a runtime exception if you had a None instead (for example, if the key was not in your map).

    A much cleaner way is to use Option.foreach or Option.map like this:

    scala> val map = Map(1 -> 2)
    map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)
    
    scala> map.get(1).foreach( i => println("Got: " + i))
    Got: 2
    
    scala> map.get(2).foreach( i => println("Got: " + i))
    
    scala> 
    

    As you can see, this allows you to execute a statement if and only if you have an actual value. If the Option is None instead, nothing will happen.

    Finally, it is also popular to use pattern matching on Option types like this:

    scala> map.get(1) match {
         |  case Some(i) => println("Got something")
         |  case None => println("Got nothing")
         | }
    Got something
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Scala's command line REPL: def foo(x: Int): Unit = {} def foo(x: String):
My company is beginning to write some code using Scala. I've been moved onto
val m = scala.collection.mutable.Map[String, Int]() // this doesn't work m += (foo, 2) //
I'm using Scala's remote actors, but round-trip time (even of a trivial message) is
( Note : I'm using Scala 2.7.7 here, not 2.8). I'm doing something pretty
I am trying to parallelise a code using scala actors. That is my first
I'm working on a project using Scala running Selenium tests as part of a
I am generating the scala AST using the following code: val setting = new
How can I create a node in Scala without using literals? What I need
I'm using Netbeans to write Scala and Java. Netbeans generated a .jar file for

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.