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
4
votes
7 answers

Scala - Option.getOrElse() "type erasure"?

I am trying to use the Option.getOrElse() method but it returns etiher Any or ScalaObject instead of an instance of the correct class that the Option was parametrized with. I can't find any mention about this problem and it does not seem like it…
noncom
  • 4,962
  • 3
  • 42
  • 70
4
votes
2 answers

Filtering on Scala Option[Set]

I have a Scala value of type Option[Set[String]] that I am trying to use in a collection filter method: val opt: Option[Set[String]] = ... collection.filter { value => opt match { case Some(set) => set.contains(value) case None => true …
Ralph
  • 31,584
  • 38
  • 145
  • 282
3
votes
4 answers

Confused by "Scala in Depth" Option example

Chapter 2 of the new Manning book, "Scala in Depth" by Josh Sueresh is posted here. In reading the article, I came across this bit of code: def getTemporaryDirectory(tmpArg : Option[String]) : java.io.File = { tmpArg.map(name => new…
chaotic3quilibrium
  • 5,661
  • 8
  • 53
  • 86
3
votes
3 answers

Producing a partially applied function from method of type in an Option

Suppose I'm writing a GUI class Kitteh (val age: Int) { require (age < 5) def saveMeow(file: File) = { /* implementation */ } def savePurr(file: File) = { /* implementation */ } } The frame has a field for the current Kitteh, which is an…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
3
votes
0 answers

Scala: Updating a field of an object in an Option

What is idiomatic way of updating a field in an object inside a scala option? flag is a boolean field in item,and I just want to reverse its value: val newOption = itemOption.map(item => item.copy(flag = !item.flag)) Is this correct way?
Mandroid
  • 6,200
  • 12
  • 64
  • 134
3
votes
3 answers

Options in Scala with Boolean default value with getOrElse

I have a map with some pairs and I want to assign a default value(Boolean) for a key, the default value for a key is a string when I used getOrElse and I'm looking for a way to do it for a int or boolean. for Eg: val someMap= Map("key1"->"value1",…
knowledge_seeker
  • 362
  • 3
  • 20
3
votes
2 answers

Trouble serializing optional value class instances with json4s

I'm trying to serialize a case class with optional value-class field to JSON using json4s. So far, I'm not able to get the optional value-class field rendered correctly (see the snippet below with examples). I tried json-native and json-jackson…
Roman
  • 64,384
  • 92
  • 238
  • 332
3
votes
2 answers

How to yield multiple values?

I have a for comprehension like: val ao = Option(1) val bo = Option(2) val (x,y) = for (a <- ao; b <- bo) yield (a+b, b+a*2) However this does not work. For comprehension returns Option[(Int,Int)] but cannot be assigned to individual x and y. If I…
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
3
votes
2 answers

Where is the method that boxes an AnyRef into an Option?

In this blogpost by James Iry, he writes: Plus, Scala has an "option" method that promotes a value to either Some(value) or None depending on whether it's null or not ... I can't seem to find this option method anywhere in the scaladoc. Iulian…
Frankie Ribery
  • 11,933
  • 14
  • 50
  • 64
3
votes
4 answers

Behaviour of Options inside for comprehension is Scala

Two newbie questions. It seems that for comprehension knows about Options and can skip automatically None and unwrap Some, e.g. val x = Map("a" -> List(1,2,3), "b" -> List(4,5,6), "c" -> List(7,8,9)) val r = for {map_key <- List("WRONG_KEY", "a",…
Alexey Kalmykov
  • 1,958
  • 2
  • 15
  • 27
3
votes
1 answer

Get Future objects from Future Options in Scala

I am new to Scala from Java so the functional programming thing is still a bit difficult for me to understand. I have a project in Play framework. I need to query the database to get rows with ids and display them in a html template. Here is my…
3
votes
1 answer

Asymmetry of (Some (x), None).min and max

Accidentally, I observed an asymmetry. Let's have a list: val li = List (Some (3), Some (2), None, Some (9)) li: List[Option[Int]] = List(Some(3), Some(2), None, Some(9)) scala> li.max res54: Option[Int] = Some(9) Ok - Some(9) is bigger than…
user unknown
  • 35,537
  • 11
  • 75
  • 121
3
votes
2 answers

When to use Option#orNull

As I understand we should avoid using null in Scala. And if some field is not logically valid to have "no-value" then we should not use Option to avoid overusing of Options. So lets look to code. I have class case class User (name: String) and I…
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
3
votes
2 answers

Best Practices for nested Scala Option fields?

I've got a number of nested objects, all wrapped in a Scala Option type. Elsewhere in my project I'm having to call an attribute that is embedded some 5 levels deep (some of which are Lists), each time making a call to .get. This way I end up with…
GroomedGorilla
  • 920
  • 2
  • 10
  • 30
3
votes
1 answer

Dealing Options from a Map.get in Scala

I've a Scala Map that contains a bunch of parameters that I get in a HTTP request. val queryParams = Map( ("keyword" -> "k"), ("from" -> "f"), ("to" -> "t"), ("limit" -> "l") ) I've a method that takes all these parameters. def someMethod(…
Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161