Questions tagged [scala-option]

Option in the Scala language is a container for zero or one element of a given type.

Option in is a container for zero or one element of a given type.

Related tags

232 questions
31
votes
4 answers

Scala Option - Getting rid of if (opt.isDefined) {}

My code is becoming littered with the following code pattern: val opt = somethingReturningAnOpt if (opt.isDefinedAt) { val actualThingIWant = opt.get } Is there some way to simplify this? (it seems needlessly complex and a code smell). Ideally…
laurencer
  • 1,680
  • 1
  • 19
  • 29
30
votes
1 answer

Does Scala have an API method that converts a Seq[Option[T]] to Seq[T]?

Is there a Scala API method to convert a Seq[Option[T]] -> Seq[T]? You can do this manually via: seq.filter(_.isDefined).map(_.get) Wondering if there is a method that does the above in the general API.
ssanj
  • 2,169
  • 3
  • 19
  • 28
30
votes
5 answers

Convert a List of Options to an Option of List using Scalaz

I want to transform a List[Option[T]] into a Option[List[T]]. The signature type of the function is def lo2ol[T](lo: List[Option[T]]): Option[List[T]] The expected behavior is to map a list that contains only Somes into a Some containing a list of…
30
votes
6 answers

A better way to test the value of an Option?

I often find myself with an Option[T] for some type T and wish to test the value of the option against some value. For example: val opt = Some("oxbow") if (opt.isDefined && opt.get == "lakes") //do something The following code is equivalent and…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
27
votes
3 answers

Combining 2 Options into 1

Is there a predefined function x in Scala that combine 2 Options so that Some(a) x None => Some(a) None x Some(b) => Some(b) None x None => None
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
26
votes
2 answers

Check for None in Scala Option type isEmpty method

I'm using the Option Type's isEmpty method to check if there is no value. I do not want to use the case match as in my situation, I just want to check if there is None as I would throw an error to the caller. But the isEmpty method fails even though…
joesan
  • 13,963
  • 27
  • 95
  • 232
25
votes
2 answers

How to call scala's Option constructors from Java

I am working on a mixed java/scala project, and I am trying to call a scala object's method from Java. This method takes an Option[Double] as a parameter. I thought this would work: Double doubleValue = new Double(1.0); scalaObj.scalaMethod(new…
pkaeding
  • 36,513
  • 30
  • 103
  • 141
24
votes
3 answers

How to convert X => Option[R] to PartialFunction[X,R]

As long as we have a PartialFunction[X,R] it's very easy to convert it to a function returning Option[R], e.g. def pfToOptf[X, R](f: PartialFunction[X,R])(x: X) = if (f.isDefinedAt(x)) Some(f(x)) else None However, what if the task is…
Alexander Azarov
  • 12,971
  • 2
  • 50
  • 54
23
votes
5 answers

Convert a List into an Option if it is populated

I have a method that should convert a list to an Option of an object, or None if the list is empty. def listToOption(myList: List[Foo]): Option[Bar] = { if(myList.nonEmpty) Some(Bar(myList)) else None } case class Bar(fooList: List[Foo])…
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
23
votes
2 answers

Difference between Java Optional and Scala Option

At the very end, this article introducing to new Java 8 Optional, states that Optional is not nearly as powerful as Option[T] in Scala (but at least it doesn’t allow wrapping null). The API is not as straightforward as null-handling and…
giampaolo
  • 6,906
  • 5
  • 45
  • 73
22
votes
5 answers

Scala: Making implicit conversion A->B work for Option[A] -> Option[B]

I'm trying to write a function which re-uses the implicit conversions which I have for Object A -> Object B when they are wrapped in an Option in a generic way so that Option[A] -> Option[B] conversions also work. What I've come up with is: implicit…
Albert
  • 2,125
  • 1
  • 19
  • 24
22
votes
5 answers

In Scala, is there a pre-existing library function for converting exceptions to Options?

This is basically to wrap java factory methods which throw exceptions if the item can't be created based on the inputs. I'm looking for something in the base library like: def exceptionToOption[A](f: => A):Option[A] ={ try{ Some(f)} …
Noel Kennedy
  • 12,128
  • 3
  • 40
  • 57
22
votes
8 answers

How can I (best) convert an Option into a Try?

How can I (best) convert an Option returned by a method call into a Try (by preference, although an Either or a scalaz \/ or even a Validation might be OK) including specifying a Failure value if appropriate? For example, I have the following code,…
Shadowlands
  • 14,994
  • 4
  • 45
  • 43
18
votes
1 answer

Why does Option not extend the Iterable trait directly?

Option is implicitly convertible to an Iterable - but why does it not just just implement Iterable directly: def iterator = new Iterator[A] { var end = !isDefined def next() = { val n = if (end) throw new NoSuchElementException() else get …
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
17
votes
2 answers

Scala: filtering a collection of Options

Say I have a function that checks whether some operation is applicable to an instance of A and, if so, returns an instance of B or None: def checker[A,B]( a: A ) : Option[B] = ... Now I want to form a new collection that contains all valid…
Gregor Scheidt
  • 3,952
  • 4
  • 24
  • 28
1
2
3
15 16