Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning scala empty array

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?

like image 691
PRASANTH Avatar asked Oct 19 '12 07:10

PRASANTH


1 Answers

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)
like image 57
tomferon Avatar answered Sep 21 '22 21:09

tomferon