Lets look at the following code:
var myVar:Any = "Hello World!!!" /*Do not let the scala compiler to deduce the type. Force the type to "Any".*/
//Check for type of myVar now!
println(myVar.getClass()) // class java.lang.String
//Now reassign with a numeric value!
myVar = 42
//Check for type of myVar now!
println(myVar.getClass()) //class java.lang.Integer
Hence, by forcing the type to Any at the point of definition will allow us variables to be assigned with any type of values!
No comments:
Post a Comment