I'm totally new to Scala. Here I have tried to assign a empty array to a variable, it was successful. But when I tried to append an integer element to the variable an error occured as below:
var c=Array()
c: Array[Nothing] = Array()
scala> c=Array(1)
<console>:8: error: type mismatch;
found : Int(1)
required: Nothing
c=Array(1)
^
What is the reason for this?
When you do var c = Array()
, Scala computes the type as Array[Nothing]
and therefore you can't reassign it with a Array[Int]
. What you can do is:
var c : Array[Any] = Array()
c = Array(1)
or
var c : Array[Int] = Array()
c = Array(1)