0

In my program, I have:

val f = Source.fromURL(url)
var lineList
try lineList = f.getLines.toList finally f.close()

I get compilation error:

Error:(13, 1) '=' expected but ';' found. try lineList = f.getLines.toList finally f.close()

What mistake am I making?In fact I am doing what error message asks me to do.

Mandroid
  • 6,200
  • 12
  • 64
  • 134

2 Answers2

1

Since you're not assigning something to lineList it needs a type definition, because the compiler cannot infer the type.

var lineList: List[String]

and if your not declaring an abstract class you need to assign it something, e.g. with the wildcard operator _

var lineList: List[String] = _
Saskia
  • 1,046
  • 1
  • 7
  • 23
  • But doesn't this means we are creating a list object,even though we won't be using it as lineList would be assigned to a another object. – Mandroid Apr 17 '18 at 13:35
  • @Mandroid no you aren't creating any object, just declaring the variable. You need to specify what type the variable should be. – puhlen Apr 17 '18 at 13:40
  • In Scasti I had to assign it something. But that's probably due to how Scasti is set up. – Saskia Apr 18 '18 at 06:40
0

In scala, you cannot just name a variable like in java. Variable needs to be initialized.

If you want to create a variable without initialization you need to specify the type:

var a = _ (doesn t work)

var a: Int = _ (works)

http://allaboutscala.com/tutorials/chapter-2-learning-basics-scala-programming/scala-basic-tutorial-declare-variables-types/

It has a good explanation

pramesh
  • 1,914
  • 1
  • 19
  • 30