Q: scala function return type either or I've a function in scala which returns either null or List[Double] or List[List[List[Double]]] as per condition. I used the keyword Any to define the return type and it works for me but if i tried to use the properties of List like 'length' on the returned value it gives me error as value length is not a member of Any. Currently, i've defined function as :
def extract_val(tuple: Tuple3[String,List[Double],List[List[List[Double]]]]): (Any) ={
     /*
        do something here
     */
}

I'm trying to find out some way such that i can define either or return type in my function definition as :
def extract_val(tuple: Tuple3[String,List[Double],List[List[List[Double]]]]): (either Type A or either Type B) ={
     /*
        if something :
          return null
        elif something:
          return Type A
        elif something 
          return Type B
     */
}

I used the OR operator as (Type A || Type B) but i got some error as not found type ||. Any help would be really useful.

A: If you know that your return type is always going to be nothing (null) or A or B, then using Option[Either[A,B]] is the fastest way to do this. 
The main limitation of this solution is that it will be harder to expand it for more types, C, D, etc. 
If expandability is what you need, you can implement your own OneOf type. You can do this relatively easy in Scala.
sealed trait OneOf[A,B,C]
case class First[A,B,C](a: A) extends OneOf[A,B,C]
case class Second[A,B,C](b: B) extends OneOf[A,B,C]
case class Third[A,B,C](c: C) extends OneOf[A,B,C]

Here is a simple (toy) use case:
def x(i: Int): OneOf[Int,Boolean,Double] =  i match {
   case 1 => First(10)
   case 2 => Second(true)
   case _ => Third(0.2)
}

scala> x(2)
res1: OneOf[Int,Boolean,Double] = Second(true)

scala> x(1)
res2: OneOf[Int,Boolean,Double] = First(10)

scala> x(2)
res3: OneOf[Int,Boolean,Double] = Second(true)

scala> x(3)
res4: OneOf[Int,Boolean,Double] = Third(0.2)

Here is a method that takes a OneOf and handles each option differently. 
def takeOneOf[A,B,C](x: OneOf[A,B,C]) = x match {
     case First(a) => println(s"A=$a")
     case Second(b) => println(s"B=$b")
     case Third(c) => println(s"C=$c")
}

