Home > Mobile >  how to know Return type inferred in scala
how to know Return type inferred in scala

Time:11-17

val a=10; val b=if(a==5) println("hello") else (1,"hi")

What will be return type inferred in this case? And How?

CodePudding user response:

Presumably you're asking for the type of b, which in this case is Any.

if (a == 5) println("hello") else (1, "hi")

gets typed as follows:

  • For an if expression, the type is the least upper bound of the two possible branches: if the consequent expression (println("hello") in this case) has type A and the alternative expression ((1, "hi")) has type B, then it is the type C such that A and B are both non-strict subtypes of C and there is no type D which is a strict subtype of C where A and B are non-strict subtypes of D (non-strict means that we can consider a type to be a subtype of itself; strict means we can't).

  • The type of println("hello") is Unit

  • The type of (1, "hi") is a Tuple2[Int, String]

  • Unit's supertypes are AnyVal and Any

  • Tuple2's supertypes are Serializable, Product2[Int, String], Product, Equals, AnyRef, and Any (technically, Product2's and Tuple2's covariance means that there are some more supertypes (e.g. Tuple2[AnyVal, String], Tuple2[Int, AnyRef], Tuple2[Any, Any]), but those don't end up being relevant here)

  • The least-upper-bound is therefore Any, so the type of the if expression and thus of b is Any

You can demonstrate this in a REPL, e.g. sbt console

scala> :paste
// Entering paste mode (ctrl-D to finish)

val a=10; val b=if(a==5) println("hello") else (1,"hi")

// Exiting paste mode, now interpreting.

a: Int = 10
b: Any = (1,hi)

Note that even though the predicate a == 5 will never be true and thus the consequent branch will never be taken, the typer does not take that into account.

Note also that if you had a method with that body:

def someMethod = {
  val a = 10
  val b = if(a==5) println("hello") else (1,"hi")
}

The result type of that method would be Unit, because the result type is the type of the last expression in the method and an assignment (being a side-effect) has the type Unit.

  • Related