The following is a very simple scala code (upper.scala):
/**
* Created on 7/6/2015.
*/
class Upper {
def upper(strings: String*): Seq[String] = {
strings.map((s: String) => s.toUpperCase())
}
}
val up = new Upper
println(up.upper("Hello World!"))
Execute it in command line, it is OK:
[root@Fedora ~]# scala upper.scala
ArrayBuffer(HELLO WORLD!)
But when executing it with Run 'Scala console' command in Intellij, it prompts the following error:
Error:(10, 1) expected class or object definition
val up = new Upper
^
Error:(11, 1) expected class or object definition
println(up.upper("Hello World!"))
^
The screen shot likes this:
Could anyone give some clue on this issue?
You're defining a value and calling for function right inside file (build unit), which is not allowed in Scala. It worked in REPL because it just implicitly wraps it into some default object, like:
object $iw {
class Upper {
def upper(strings: String*): Seq[String] = {
strings.map((s: String) => s.toUpperCase())
}
}
val up = new Upper
println(up.upper("Hello World!"))
}
Your approach also won't work with plain scalac if you're looking for clean check.
Solution: just use App trait, which will generate main method automatically so you can run the code:
object MyApp extends App {
val up = new Upper
println(up.upper("Hello World!"))
}
I think that this is an issue for scala scripts in intellij, One way of running this scripts is a little bit tricky. From my point of view making and object that extends and app is building an scala application not a scala script
First remove the make item, in the configuration:

Then run the scala console

Next you must select the code and send to console

and finally, you can see the result in the console

I hope that this works, for running your code script, without making and App
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With