Search code examples
scalaintellij-ideavar

Scala statements and Expression - Var vs Val


This works fine in the Scala REPL:

scala> var g,h = 20
g: Int = 20
h: Int = 20

But, in an InteliJ worksheet with just var g,h = 20, I get an error:

Error:(1, 6) constructor cannot be instantiated to expected type; found : (T1, T2) required: Int var (a,b) = { 20;};} 

If I change it from var to val, it works fine. What's going on?


Solution

  • I belive you are running it from a plain .sc file. The syntax is valid . Try it from a main(..) method or something like below . It works

    object Main extends App {
    
    var g,h = 20
    println(g)
    println(h)
    
    }