This getting started guide is intended for those who want to start working with the Scala IntelliJ IDEA plugin. Hope it gives you a good kick to start exploring it the plugin for yourself, and, of course, reading this space - you can find more useful information about other helpful plugin features, like Java/Scala cross-language interoperability which includes refactorings, coding assistance and much more.
- Right-click src folder (or press Alt-Ins in default Windows IntelliJ IDEA keymap) and select Package. Type the package name helloworld. Then, right-click the newly created package and choose Scala Class on the context menu:
- In the dialog box that opens, select the type of the Scala entity to be created - in our case, let it be Scala Object, type HelloWorld as the object name, and click OK. Here is our first Scala object.
- Now, we're going to make it runnable. There're several ways to achieve that. First off, we can extend the default scala.Application trait. With IntelliJ IDEA advanced code completion (in this case, the Scala Class Names) we don't have to type everything by ourselves. App plus Ctrl+Space gives you neat a and quick choice (see the picture below).
Or, we can use a more Java-like approach: create main() method inside of object body. Again, IntelliJ IDEA lends us a hand with Live Templates. All we do is type main and press Tab (or press Ctrl+J in the default Windows IntelliJ IDEA keymap, and select main from the suggestion list). IntelliJ IDEA expands this macro to a complete well-formed main() method body.
- Let's teach the application to do some work. If you're familiar with IntelliJ IDEA, you should probably be aware of that running a simple Java application is just a matter of pressing Ctrl+Shift+F10 (on default Windows keymap). Good news is that it works equally well for Scala applications.
All we have to do is to type the code that displays the trivial Hello World message and then press Ctrl+Shift+F10 - or right-click the editor background and choose Run HelloWorld.main on the context menu. The application output is displayed in the console.
You can also turn your Scala object into a script. Just remove all declarations from the file, leave the executable statement only - println("Hello, World!"), and run the script as described above.