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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:57:19+00:00 2026-05-23T13:57:19+00:00

Possible Duplicate: Why Option[T] ? I am a newbie on Scala and I can’t

  • 0

Possible Duplicate:
Why Option[T] ?

I am a newbie on Scala and I can’t really feel that sensation of that difference between null on java and Option on Scala .
I know that none is an object and if I write smth like that on Scala , it will go safe :

val map = Map("koko" -> "is a cat")
val string:Option[String] =map.get("other")
println(string.map(a=>println(a.toString)) )

I well get None as a result instead of throwing an exception .
It’s interesting .

However if I need the returning value not to be wrapped by Some . I will use .get to return the value .

In our example , it will throw an exceoption:

map.get("other").get.map(a=>println(a.toString))

I know I can handle this issue by using “match” .
I am taking here that I need to learn what’s that booming on option on Scala vs null on java !

What I still couldn’t get is that how could I use the advantages of Option but in case there are values existed into my variable , return the value not Some(value)

def testOption(): String = {
  val map = Map("koko" -> "is a cat")
  val string: Option[String] = map.get("koko")
  string
}

I mean is there any way to make this code works instead of updating the return value to be Option[String] !
Imagine that I have to return a String in order to be set into my bean variable that is of type String not Option[String]

How could I make that works without any kind of match !!
I guess if there smth that makes it works , that will make me understand well the power of Option.

  • 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-23T13:57:19+00:00Added an answer on May 23, 2026 at 1:57 pm

    Options are actually quite useful. In Java, it’s tough to document whether or not something can be null. In Scala, you can often assume all variables are not null.

    This gets really useful when you have some computation that can fail or may not be defined. A good instance would be if you have a webapp and there’s some user data that isn’t filled out, so the computation can’t run. If you had the user data in a map, you could do something like

    val myData = map.get(userId).map(doFunction).map(toHtml)
    println(myData.getOrElse(noDataHtml))
    

    So clearly here we never have to worry about null. If the user data is in the map, we proceed with the computation and then turn that into html. Otherwise, we’ll just print out a default html. In Java, at each stage we’d have to do a null check, but in Scala we can just chain functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What’s the difference of `./configure` option `--build`, `--host` and `--target`? This is
Possible Duplicate: String formatting options: pros and cons What's the difference between %.2f %
Possible Duplicate: String vs string in C# I know there is no difference between
Possible Duplicate: Can I multiply strings in java to repeat sequences? In Python, we
Possible Duplicate: Why Option[T]? Reference types provide the special value null meaning "absence of
Possible Duplicate: $.ajax context option I've got some code that looks like this: $.post(self.baseUrl
Possible Duplicate: Hide select option in IE using jQuery So as you can see
Possible Duplicate: How to set a java system property so that it is effective
Possible Duplicate: Set Locale programatically I would like to create an english/spanish option choice
Possible Duplicate: Why does Mac's $find not have the option -printf? Not sure what

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.