Expression, Type Inferrence & Return Value

// Here we have explicitly specified the Type
scala> val no:Int = 10
no: Int = 10

// Last statement in an expression is considered as
// return value
// Scala infers the Type when we do not explicitly
// specify the Type
scala> val result1 = if (no == 10) {
     |    "good"
     | } else {
     |    "bad"
     | }
result1: String = good

// When Scala is not able to able to infer a Specific Type to
// a variable, a type of 'Any' is assigned to that variable
scala> val result2 = if (no == 10) {
     |    "good"
     | }
result2: Any = good

No comments:

Post a Comment