This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Better Option Use?

3 replies
Greg Zoller
Joined: 2011-11-15,
User offline. Last seen 42 years 45 weeks ago.

Hello,

Is there a more concise way of doing this with an Option:

if( something.isDefined ) goDoIt( something.get )

(Yes, goDoIt() could accept an Option but that just moves the if
statement into goDoIt.)

Thanks for any ideas.
-gz

Tommy Chheng
Joined: 2010-03-06,
User offline. Last seen 1 year 23 weeks ago.
Re: Better Option Use?
You can use foreach/map on Option classes.
something.foreach(goDoIt)

On Friday, December 16, 2011, Greg Zoller wrote:
Hello,

Is there a more concise way of doing this with an Option:

if( something.isDefined ) goDoIt( something.get )

(Yes, goDoIt() could accept an Option but that just moves the if
statement into goDoIt.)

Thanks for any ideas.
-gz
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Better Option Use?
On Sat, Dec 17, 2011 at 11:04 AM, Greg Zoller <gzoller@gmail.com> wrote:
Is there a more concise way of doing this with an Option:

if( something.isDefined ) goDoIt( something.get )

(Yes, goDoIt() could accept an Option but that just moves the if
statement into goDoIt.)

something.foreach(goDoIt)
See: http://blog.tmorris.net/scalaoption-cheat-sheet/
-jason 
Greg Zoller
Joined: 2011-11-15,
User offline. Last seen 42 years 45 weeks ago.
Re: Better Option Use?

Thats a fantastic cheat sheet, thanks!

On Dec 16, 7:12 pm, Jason Zaugg wrote:
> On Sat, Dec 17, 2011 at 11:04 AM, Greg Zoller wrote:
> > Is there a more concise way of doing this with an Option:
>
> > if( something.isDefined ) goDoIt( something.get )
>
> > (Yes, goDoIt() could accept an Option but that just moves the if
> > statement into goDoIt.)
>
> something.foreach(goDoIt)
>
> See:http://blog.tmorris.net/scalaoption-cheat-sheet/
>
> -jason

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland