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
5
votes
3 answers

Convert the value in an Option to another type

I'm trying to do something that seems like it should have a straight forward syntax/function in scala. I just don't know what that is. I'm trying to convert the contained value of an Option (if it is not None) to another type. Simply I want to…
Craig Suchanec
  • 10,474
  • 3
  • 31
  • 39
5
votes
3 answers

How to match option map values at once?

is it possible to match Option[Map[String,String]] for some key at once (e.g. without nested matches)? The following snippet is how it is now: val myOption:Option[Map[String,String]] = ... myOption match { case Some(params) => params get(key)…
Johnny Everson
  • 8,343
  • 7
  • 39
  • 75
4
votes
4 answers

Clojure seq as a substitute for Scala Option[T]

Scala offers a hierarchy of classes Option[T], Some[T] extends Option[T], and None extends Option[Nothing] that I have found useful for wrapping Java method calls that can return null, among other things: val result =…
Ralph
  • 31,584
  • 38
  • 145
  • 282
4
votes
3 answers

Reading multiple variables from an object wrapped in Option[]

I have a variable obj: Option[MyObject] and want to extract multiple variables from it - if the object is not set, default values should be used. Currently I do it like this: val var1 = obj match { case Some(o) => e.var1 case _ =>…
stephanos
  • 3,319
  • 7
  • 33
  • 47
4
votes
1 answer

Some constructor with asInstanceOf

When I was writing my recent answer I also tried to solve the problem in more "functional" way, but stuck with the following problem: scala> "1".asInstanceOf[Int] java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer …
CheatEx
  • 2,082
  • 2
  • 19
  • 28
4
votes
1 answer

Scala apply cannot return Option

I recently tried using apply like a factory function: class X() { def apply() : Option[X] = if (condition) Some(new X()) else None } val x : Option[X] = X() // <- this does not work; type is mismatched For some reason apply always returns X.…
Jaacko Torus
  • 796
  • 7
  • 24
4
votes
3 answers

Scala: using None for other purposes than empty Option

According to the docs, None object is intended to "represent non-existent values". As far as I've seen it's mostly used as an empty Option. But do you think it's a good idea to use it for other purposes. For example, in my library I want to have an…
Vilius Normantas
  • 3,708
  • 6
  • 25
  • 38
4
votes
5 answers

Handling two Options in Scala

I have 2 options, and I need to take average of the values they hold. It is possible that one or both may be missing. If one of the value is missing, I would just take other one as the average. But if both are missing, I would resort to some default…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
4
votes
1 answer

How to getOrElse with another Option in Scala

Let's assume that we have an option foo1 and an option foo2: val foo1: Option[Foo] val foo2: Option[Foo] Is there an operator/function that allows me to return the value of the foo2 when foo1 is None? val finalFoo: Option[Foo] =…
Yuchen
  • 30,852
  • 26
  • 164
  • 234
4
votes
2 answers

Scala Option's collect method doesn't like my PartialFunction

I think I'm missing something: scala> Some(1) collect ({ case n if n > 0 => n + 1; case _ => 0}) res0: Option[Int] = Some(2) scala> None collect ({ case n if n > 0 => n + 1; case _ => 0}) :6: error: value > is not a member of Nothing …
pr1001
  • 21,727
  • 17
  • 79
  • 125
4
votes
3 answers

How to extract sequence elements wrapped in Option in Scala?

I am learning Scala and struggling with Option[Seq[String]] object I need to process. There is a small array of strings Seq("hello", "Scala", "!") which I need to filter against charAt(0).isUpper condition. Doing it on plain val arr = Seq("hello",…
minerals
  • 6,090
  • 17
  • 62
  • 107
4
votes
3 answers

Scala Tuple Option

If I have Scala tuple Option of the likes: (Some(1), None) (None, Some(1)) (None, None) And I want always to extract always the "Some" value if it exists, and otherwise get the None. The only way with pattern matching?
Alessandroempire
  • 1,640
  • 4
  • 31
  • 54
4
votes
2 answers

Scala Option type not inferred as expected

I'm writing a function to get an entry from a JSONObject. Since it is JSON the entry by the input name may or may not exist and so the function return an Option which will be None when the Try fails or if the value is NULL on success. The function…
broun
  • 2,483
  • 5
  • 40
  • 55
4
votes
1 answer

A better implementation for pattern matching a tree-like option structure

def leadParser(optionTuples: Option[(Option[(Option[(Option[(Option[( Option[Iterable[EmailLead]], Option[Iterable[QuestionLead]])], …
igalbenardete
  • 207
  • 2
  • 12
4
votes
1 answer

jongo / jackson deserialize scala.option in java

FOUND SOLUTION TO THE PROBLEM For the people who will be stuck like me!: in order to handle third party java or scala Objects for jackson deserialization, you can either use Mixins( but you need to reconfigure the jackson mapper or user…
popo joe
  • 910
  • 1
  • 9
  • 24