cool breeze cool breeze - 1 month ago 8
Scala Question

Yielding from a for-comp is not returning the type I want

Why is the return value a Try[Serializable] for the val maybeString? I want it to be an Option[String]

import scala.util.Try

val a1 = Try("fase".toBoolean)
val b2 = Try("100".toInt)

val maybeString: Option[String] = for {
a <- a1
b <- b2
} yield (a,b) match {
case (true, 50) => "foo"
case (false, 100) => "bar"
case _ => None
}

println(s"string is $maybeString")

Answer

Why is the return value a Try[Serializable] for the val maybeString?

Because scala compiler cannot determine the type of the element you return in the following expression:

(a,b) match {
  case (true, 50) => "foo"
  case (false, 100) => "bar"
  case _ => None
}

You return String in two cases and Option (i.e. None) in another one, so it picks up the most common type in hierarchy: Serializable.

I want it to be an Option[String]

object Test extends App {

  import scala.util.Try

  val a1 = Try("false".toBoolean)
  val b2 = Try("100".toInt)

  val maybeString: Option[String] = for {
    a <- a1.toOption
    b <- b2.toOption
  } yield (a, b) match {
    case (true, 50) => "foo"
    case (false, 100) => "bar"
  }

  println(s"string is $maybeString")
}