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

Why wrapping a generic method call with Option defers ClassCastException?

Lets say I have an array like this*: val foo: Any = 1 : Int Option(foo.asInstanceOf[String]) which fails for obvious reason: // java.lang.ClassCastException: java.lang.Integer cannot be cast to // java.lang.String // ... 48 elided Next lets…
zero323
  • 322,348
  • 103
  • 959
  • 935
15
votes
5 answers

scala return on first Some in list

I have a list l:List[T1] and currently im doing the following: myfun : T1 -> Option[T2] val x: Option[T2] = l.map{ myfun(l) }.flatten.find(_=>true) The myfun function returns None or Some, flatten throws away all the None's and find returns the…
svrist
  • 7,042
  • 7
  • 44
  • 67
15
votes
5 answers

When should I use Option.empty[A] and when should I use None in Scala?

In Scala, when I want to set something to None, I have a couple of choices: using None or Option.empty[A]. Should I just pick one and use it consistently, or are there times when I should be using one over the other? Example: scala> def f(str:…
cdmckay
  • 31,832
  • 25
  • 83
  • 114
15
votes
4 answers

Coalescing options in Scala

Most SQL implementations (this question has nothing to do with SQL, it is just an example) offer the function COALESCE(x1,x2,...,xn) which returns x1 if it is not NULL, x2 otherwise only if x2 is not NULL neither and so on. If all xi values are NULL…
15
votes
5 answers

How do I convert an option tuple to a tuple of options in Scala?

This question is the opposite of this question. val x = Some((1, 2)) val (y: Option[Int], z: Option[Int]) = ??? Both pure Scala answers and Scalaz anwers are helpful.
Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64
14
votes
3 answers

Better to return None or throw an exception when fetching URL?

I have a Scala helper method that currently tries to fetch a URL and return an Option[String] with the HTML of that webpage. If there are any exceptions (malformed url, read timeouts, etc...) or if there are any problems it returns a None. The…
James
  • 15,085
  • 25
  • 83
  • 120
14
votes
2 answers

Convert List of 1 element to Option

Let's say I have a List[T] from which I need a single element, and I'd like to convert it to an Option. val list = List(1,2,3) list.take(1).find(_=>true) // Some(1) val empty = List.empty empty.take(1).find(_=>true) // None This would appear to be…
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
13
votes
5 answers

Scala: map with two or more Options

Basically I'm looking for the most scala-like way to do the following: def sum(value1: Option[Int], value2: Option[Int]): Option[Int] = if(value1.isDefined && value2.isDefined) Some(value1.get + value2.get) else if(value1.isDefined &&…
Vilius Normantas
  • 3,708
  • 6
  • 25
  • 38
13
votes
4 answers

Any way to access the type of a Scala Option declaration at runtime using reflection?

So, I have a Scala class that looks like this: class TestClass { var value: Option[Int] = None } and I'm tackling a problem where I have a String value and I want to coerce it into that Option[Int] at runtime using reflection. So, in another…
Graham Lea
  • 5,797
  • 3
  • 40
  • 55
13
votes
5 answers

more elegant way to write if( list.nonEmpty) Some(list.max) else None?

List.max returns the "largest" element of a list based on some ordering... But if the list is empty you'll get a java.lang.UnsupportedOperationException: empty.max exception. I don't really like littering code with if statements or matches or…
nairbv
  • 4,045
  • 1
  • 24
  • 26
13
votes
4 answers

Summing up two options

Let's say I have two optional Ints (both can be Some or None): val one : Option[Int] = Some(1) val two : Option[Int] = Some(2) My question is the following: Are there any intelligent way to sum them op using Scalas brilliant collection-methods? I…
Jens Egholm
  • 2,730
  • 3
  • 22
  • 35
13
votes
4 answers

Scala Option object inside another Option object

I have a model, which has some Option fields, which contain another Option fields. For example: case class First(second: Option[Second], name: Option[String]) case class Second(third: Option[Third], title: Option[String]) case class…
psisoyev
  • 2,118
  • 1
  • 25
  • 35
13
votes
1 answer

Why does Option require explicit toList inside for loops?

Using a for loop with a simple Option works: scala> for (lst <- Some(List(1,2,3))) yield lst res68: Option[List[Int]] = Some(List(1, 2, 3)) But looping over the contents of the Option does not: scala> for (lst <- Some(List(1,2,3)); x <- lst) yield…
Lorrin
  • 1,799
  • 1
  • 16
  • 21
12
votes
6 answers

Is there a ruby equivalent to the Scala Option?

How do I model an optional value in ruby? Scala has Option[], which is what I'm looking for in ruby.
ladrl
  • 205
  • 2
  • 7
11
votes
3 answers

Which Scala methods return null instead of an Option and why?

I wonder if the standard library is completely null-free and - if not - would be interested what reasonable use-cases exist where returning null is preferable to returning some Option instance.
soc
  • 27,983
  • 20
  • 111
  • 215
1 2
3
15 16