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

  • Home
  • SEARCH
  • 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 8950311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:25:32+00:00 2026-06-15T13:25:32+00:00

As a Scala developer who also works in GWT , I welcome the addition

  • 0

As a Scala developer who also works in GWT, I welcome the addition of Optional to Guava.

One of our most common use cases of Optional is when returning optional values from methods (as suggested by the answer to What's the point of Guava's Optional class.

In scala, I often write code like this:

def someExpensiveOperation(params: Type): Option[ResultType] = ...
someExpensiveOperation(params).foreach({ val =>
  doSomethingWithVal (val)
})

Guava’s Option does not seem to allow anything more elegant than something like this:

Optional<MyType> optionalResponse = someExpensiveOperation(params);
if (optionalResponse.isPresent()) {
    doSomethingWithVal(optionalResponse.get())
}

The local variable is redundant, and it requires repeating a pattern which could be abstracted (the if (optional.isPresent()) { doSomethingWith(optional.get()) } ).

The other option would be to call the method which returns an Optional twice:

if (someExpensiveOperation(params).isPresent()) {
    doSomethingWithVal(someExpensiveOperation(params).get())
}

But that is clearly undesirable, since it invoked an expensive operation multiple times unnecessarily.

I’m curious how other people have handled this very-common case (perhaps by writing a static utility method like <T>useIfPresent(Optional<T> val, Closure<? super T> closure)?) or if anyone has found more elegant solutions.

Also, if anyone knows why a method like Optional.foreach(Closure<? super T> closure) (but hopefully better named) was omitted, I would be curious to hear the rationale.

  • 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-15T13:25:33+00:00Added an answer on June 15, 2026 at 1:25 pm

    It’s not there because we feel the anonymous class awkwardness of writing a Closure is more awkward and less readable — at least in Java, not necessarily in Scala — than the local variable and the if statement that you’ve already written.

    That said, another alternative is

    for (Foo x : someExpensiveOperation().asSet()) {
      // do stuff with x
    }
    

    Note that asSet is necessary here — Optional very deliberately does not itself implement Iterable.

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

Sidebar

Related Questions

Is it possible for a developer to use Scala without knowing Java? Is it
I'm a strong Java developer who has very recently started trying to pick up
As a Java developer who is reading Apple's Objective-C 2.0 documentation: I wonder what
Being new to scala and a current java developer, scala was designed to encourage
I am a newbie Scala developer trying to figure out how to design a
I am a Java developer trying to learn Class creation in Scala. I create
I'm developer of Robocode engine. We would like to make Robocode multilingual and Scala
I build a huge graph in JVM (Scala) which I want to use repeatedly,
I am an ActionScript 3 developer who is just making his first way in
On large-scale Java/.Net Enterprise projects, does every developer need to have all the components/libraries/dependencies

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.