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
11
votes
2 answers

Proper way to construct Option object: Option(value) vs Some(value)

What is the pros and cons of 2 ways of initiating Option objects: 1. def getAmount: Option[Int] = { val a: Int = 1 Option(a) } 2. def getAmount: Option[Int] = { val a: Int = 1 Some(a) } Which should I use?
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
11
votes
2 answers

Scala: How can I explicitly compare two Options?

If I have two Options such as val a = Option(2) val b = Option(1) I can write List(a,b).sorted and it sorts correctly by inserting an implicit Ordering. How can I get a reference to this Ordering so I can call compare(a,b) and get the result? I'd…
Sam
  • 1,260
  • 2
  • 11
  • 32
11
votes
3 answers

Slick left outer join fetching whole joined row as option

My join looks like this: def byIdWithImage = for { userId <- Parameters[Long] (user, image) <- Users leftJoin RemoteImages on (_.imageId === _.id) if user.id === userId } yield (user, image) but slick fails at runtime when user.imageId is…
Somatik
  • 4,723
  • 3
  • 37
  • 49
11
votes
9 answers

How do I find the min() or max() of two Option[Int]

How would you find minValue below? I have my own solution but want to see how others would do it. val i1: Option[Int] = ... val i2: Option[Int] = ... val defaultValue: Int = ... val minValue = ?
Graham Lea
  • 5,797
  • 3
  • 40
  • 55
10
votes
2 answers

Scala: Does Option[Boolean] makes sense?

So an Option[Int] or Option[String] or for that matter Option[A] results in Some(A) or None, but a Boolean is different as it inherently represents dual states (true/false), does it makes sense to have Option[Boolean]? I face this frequently when a…
iyerland
  • 632
  • 2
  • 10
  • 24
9
votes
3 answers

Method call with option value or default parameter in Scala

I'm rather new to Scala and stumbled upon a small little issue that keeps bothering me. Let's say there is some method with default parameter def foo(v: Any = "default"): String = s"called with parameter '$v'" and an Option val opt:…
ABika
  • 597
  • 1
  • 5
  • 11
9
votes
3 answers

Scala for comprehension with future and options

object Main extends App { val p1 = Promise[Option[String]]() val p2 = Promise[Option[String]]() val f1 = p1.future val f2 = p2.future val res = (for{ file1Opt <- f1 file2Opt <- f2 file1 <- file1Opt file2 <- file2Opt }…
An Illusion
  • 769
  • 1
  • 10
  • 24
8
votes
1 answer

Methods versus Function and implicits in Scala

Let's declare a def and an equivalent function as a val: scala> def optional(x:Int):Option[String] = None optional: (x: Int)Option[String] scala> val optional2:(Int)=>Option[String] = (i:Int) => None optional2: Int => Option[String] =…
Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
8
votes
3 answers

Examples of using some Scala Option methods

I have read the blog post recommended me here. Now I wonder what some those methods are useful for. Can you show examples of using forall (as opposed to foreach) and toList of Option?
Michael
  • 10,185
  • 12
  • 59
  • 110
8
votes
2 answers

Why does the Option's orNull method have this superfluous implicit argument?

I wonder what is the reason for the (implicit ev: Null <:< A1) here: sealed abstract class Option[+A] extends Product with Serializable { def orNull[A1 >: A](implicit ev: Null <:< A1): A1 = this getOrElse null ... } Wouldn't def orNull[A]: A…
soc
  • 27,983
  • 20
  • 111
  • 215
8
votes
4 answers

How can I reverse of flow of Option Monad?

say, I have a bunch of "validation" functions that return None if there is no error, otherwise it return Some(String) specifying the error message. Something like the following ... def validate1:Option[String] def validate2:Option[String] def…
sanjib
  • 593
  • 1
  • 4
  • 7
8
votes
4 answers

Avoiding deeply nested Option cascades in Scala

Say I have three database access functions foo, bar, and baz that can each return Option[A] where A is some model class, and the calls depend on each other. I would like to call the functions sequentially and in each case, return an appropriate…
Ralph
  • 31,584
  • 38
  • 145
  • 282
8
votes
4 answers

Handle Scala Option idiomatically

What is the more idiomatic way to handle an Option, map / getOrElse, or match? val x = option map { value => Math.cos(value) + Math.sin(value) } getOrElse { .5 } or val x = option match { case Some(value) => Math.cos(value) +…
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
7
votes
4 answers

How do I pull apart Case Classes filled with Options in Scala

I'm very new to Scala and I'm still trying to get used to the syntax and style, so this is probably a very simple question. I'm working with a codebase where there are lots of case classes populated with Options like so: case class Person( pants:…
Adam Fraser
  • 6,255
  • 10
  • 42
  • 54
7
votes
2 answers

Processing optional xml attributes in Scala

I have code that reads an XML file. Some of the attributes of elements that I need to process are optional. I am trying to use Option[T] to manage them. I have written the following to pimp the NodeSeq type returned by the \ Node operator: class…
Ralph
  • 31,584
  • 38
  • 145
  • 282