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
7
votes
1 answer

Nested Scala matchers why Some(Some(1),1) can't match?

It seems that nested matching doesn't work, which is a strange limitation. An example of the behaviour follows: Some(Some(1),2) match { | case Some(Some(a),b) => a | case e => e | } :9: error: wrong number of arguments for : (x:…
Bryan Hunt
  • 3,685
  • 2
  • 24
  • 36
7
votes
1 answer

how to read json with an optional field serialized by a missing field, in upickle

I use upickle for serializing json in scalajs. I need to be able to parse optional fields, represented by a null value and by a missing field (standard json on the web). With OptionPickler, I can accept nullable items. However, how can I accept…
David Portabella
  • 12,390
  • 27
  • 101
  • 182
7
votes
5 answers

Using Option all over the place feels a bit awkward. Am I doing something wrong?

As a result of articles I read about the Option class which helps you avoid NullPointerException's, I started to use it all over the place. Imagine something like this: var file:Option[File] = None and later when I use it: val actualFile =…
Geo
  • 93,257
  • 117
  • 344
  • 520
7
votes
1 answer

In what way is Scala's Option fold a catamorphism?

The answer to this question suggests that the fold method on Option in Scala is a catamoprhism. From the wikipedia a catamophism is "the unique homomorphism from an initial algebra into some other algebra. The concept has been applied to functional…
7
votes
2 answers

Lazily coalesce Options in Scala

I have several ways of calculating a value, in decreasing preference. firstWay() second() + way() orA(thirdWay()) Each of these returns an Option. I want to "coalesce" these and get an Option which the the value returned by the first Some of these,…
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
6
votes
2 answers

Scala: selecting function returning Option versus PartialFunction

I'm a relative Scala beginner and would like some advice on how to proceed on an implementation that seems like it can be done either with a function returning Option or with PartialFunction. I've read all the related posts I could find (see bottom…
Gregor Scheidt
  • 3,952
  • 4
  • 24
  • 28
6
votes
2 answers

Best way to score current extremum in collection type

I’m currently a little tired so I might be missing the obvious. I have a var _minVal: Option[Double], which shall hold the minimal value contained in a collection of Doubles (or None, if the collection is empty) When adding a new item to the…
flying sheep
  • 8,475
  • 5
  • 56
  • 73
6
votes
3 answers

Call a method on the value of Scala Option if present

I am trying to figure out the best way to refactor the following code to eliminate the use of Option.get(). I know that using the get method is considered bad practice. if (myConnection.isDefined) { myConnection.get.close } where myConnection is…
JoeMjr2
  • 3,804
  • 4
  • 34
  • 62
6
votes
3 answers

Working with options in Scala (best practices)

I have a method that I wrote to enrich person data by performing an API call and adding the enriched data. I have this case class: case class Person(personData: PersonData, dataEnrichment: Option[DataEnrichment]) My method is supposed to return…
MNY
  • 1,506
  • 5
  • 21
  • 34
6
votes
1 answer

Scala most elegant way to handle option and throw exception from Scala Map

I have a map and want to: retrieve value without handling Option log a message when there is no such the key. nice to return a default value ( in addition to log a message) when the key is not present. This is optional because when the code fails…
Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110
6
votes
1 answer

How to convert/wrap a sequence in scala to an Option[Seq] so that if the list is empty, the Option is None

I could do this with an if an statement, but there is probably a "scala" way to do this. def notScalaConv(in: Seq[String]): Option[Seq[String]] = { if (in.isEmpty) None else Option(in) }
marathon
  • 7,881
  • 17
  • 74
  • 137
6
votes
3 answers

Calling Java API from Scala with null argument

I have some Scala code that needs to call a Java API The Java API takes arguments that may be null. My Scala, of course, uses Option. For example, let's say I have a Java object constructor Foo(Integer) where the Integer may be null. I want to…
opus111
  • 2,744
  • 4
  • 25
  • 41
6
votes
5 answers

How do you stop building an Option[Collection] upon reaching the first None?

When building up a collection inside an Option, each attempt to make the next member of the collection might fail, making the collection as a whole a failure, too. Upon the first failure to make a member, I'd like to give up immediately and return…
Ben Kovitz
  • 4,920
  • 1
  • 22
  • 50
6
votes
2 answers

Scala - Match Tuple of Options

I have: val foo = Some(List(1, 2, 3)) -> Some("y") I would like to cast match it: foo match { case (Some(x), Some(y)) => println(x + " " + y) case _ => println("error") This works fine for Some(List(1, 2, 3) -> Some("score")) but fails for…
Karsten
  • 882
  • 6
  • 18
6
votes
5 answers

Chaining scala Try instances that contain Options

I am trying to find a cleaner way to express code that looks similar to this: def method1: Try[Option[String]] = ??? def method2: Try[Option[String]] = ??? def method3: Try[Option[String]] = ??? method1 match { case f: Failure[Option[String]] =>…
Spencer Stejskal
  • 498
  • 1
  • 6
  • 15