Mandroid
Mandroid

Reputation: 7524

Intellij hint for output type of a scala method

I have written a method in scala program in Intellij like this:

def myMethod(c: Option[C]) = {
    val x = ...
    val y = ...
    c.filter(...)
     .map(_ => {
          (x,y) match {
             case (a,b) => "val1"
             case (c,d) => "val2"
             case (_, _) => "val3"
          }
       })
}

Intellij hints that output type of this method is Option[String]. But as I have used filter method , which may return None, so is not this hint wrong?

Upvotes: 1

Views: 108

Answers (1)

jwvh
jwvh

Reputation: 51271

object None extends Option[Nothing].

Nothing is a "bottom type", i.e. it inherits from everything.

Option is covariant on its type parameter.

That means that None is a descendant from Option[X] for any and every possible X.

Thus the LUB (Least Upper Bound) between None and Option[String] is Option[String].

Upvotes: 2

Related Questions