Weird Scala issue with 10.0.2

(IDEA newbie here, sorry if this is a FAQ or a silly question, but I didn't find a solution with a half an hour of googling)

I just updated to 10.0.2 and my Scala stuff stopped working because now IDEA will not recognize my Scala object as a runnable unless the first character of the file is uppercase.

Here's how I can reproduce the issue:
- Create a new project (from scratch) and assign it the Scala 2.8.1 facet. Scala distribution resides in scala/ in my home directory. I use OSX if that makes any difference.
- I create a new Scala class Test in the project src directory, and replace the autogenerated contents with the three lines:

object Test {
   def main(args: Array[String])  = true
}

At this point the new class shows up in the project view accompanied with the yellow "O" icon, and the "Run Test.scala" option is not available in the popup menu of the file.

Now, if I capitalize the first letter of the word "object" above, ie. I have "Object Test ...", then the icon changes to the red Scala logo with a cog, and I can run it in the popup menu. However, the attempt will naturally fail with the error:

.../IdeaProjects/Test/src/Test.scala:1: error: value Test is not a member of object java.lang.Object
Object Test {
^

Process finished with exit code 1

If I change the uppercase O back to lowercase, the icon changes back to yellow "O" and I can no longer run the class/project. When I trigger the previously generated run configuration IDEA tells me "Error running Test.scala: Scala script file not found." even though I did not change the name of the file or the class.

Anyone know what's wrong?

6 comments
Comment actions Permalink

Alex,

The issue you're seeing have to do with the difference between Scala scripts (no package declaration and "loose" code at the top level like defs and vals or plain old evaluable expressions).

Scala scripts are signified by the Scala stairway icon (hard to make out) overlayed with the cogwheel. Only Scala scripts get the green Run arrow.

When you write

object Test { def main(args: Array[String]): Unit = { ... } }

... you're not writing a script. However, when you change that to

Object Test { def main(args: Array[String]): Unit = { ... } }

...you're changing the entire interpretation of this code. It is now (syntactically) deemed a script, but it will not compile (so technically it's not Scala at all, it's just a bunch of text...).


Randall Schulz

0
Comment actions Permalink

Thanks for the reply, that makes sense.

But I still don't understand how do I create the iconic hello world Scala object that is runnable in IDEA 10.0.2?

0
Comment actions Permalink

A file containing this:

-==-

println("Hello, world.")
-==-

That's a Scala script.

If you want to compile it to produce a runnable Scala program, use something like this:

-==-

package stone.alex.world


object Hello {

def

main(args: Array[String]): Unit =

println("Hello, world.")

}

-==-


Randall Schulz

0
Comment actions Permalink

Okay, I guess I asked the wrong question, sorry :)

How do I create a run configuration for the program? Ie. I want it to do what

scala stone.alex.world.Hello

does in the command line.

Again, apologies, these are trivial issues but I'm a total IDEA newbie and I can't seem get the run configuration dialog to do what I want.

0
Comment actions Permalink

Right mouse button menu -> "Run Hello.main()" entry.
Pressing Ctrl-Shift-F10 while you are in the body of the main() method should work as well.

I think the previous example that you quoted ("def main(args: Array[String])  = true") does not have the right signature for a Scala main() method, but I'm not 100% sure.

Note that there is a dedicated forum for the IntelliJ Scala plugin:
http://devnet.jetbrains.net/community/idea/scala

-tt

0
Comment actions Permalink

Thanks for the tip about the Scala plugin forum, I'll ask further questions there.

0

Please sign in to leave a comment.