Specifying Explicit Type : Type annotation

PS:45, psn > a15 > a70
There are situations where type cannot be inferred by Scala. The process of specifying explicit type in Scala is called as Type Annotation. Here are scenarios where Type Annotation is required
  1. When no value is assigned for a variable
  2. Method parameters require Type Annotation
scala> //Example : Type Annotation for Function Parameter

scala> def fn(val no) = "This is a demo"
<console>:1: error: identifier expected but 'val' found.
       def fn(val no) = "This is a demo"
              ^

scala> def fn(no) = "This is a demo"
<console>:1: error: ':' expected but ')' found.
       def fn(no) = "This is a demo"
                ^

scala> def fn(no:Int) = "This is a demo"
fn: (no: Int)String

No comments:

Post a Comment