You don't need to already know any syntax or even definitions of terms like higher-order functions. I don't teach everything about programming, but if you feel comfortable with some very basic programming concepts, I'll take it from there.
What is Scala?
Scala is a programming language that runs on the Java Virtual Machine (JVM). Scala has been around for almost as long at Java. Scala was originally written as an better alternative to Java. Scala can also run on other VM's, but the JVM seems to be the most common platform for Scala. Because Scala runs on the JVM, it was designed to interact with Java and existing Java projects with minimal fuss. Scala's Java interop is excellent and easy.
Scala itself is a hybrid between Object-Oriented Programming (OOP) and Functional Programming (FP). Don't worry if you don't fully understand what Object-Oriented Programming and Functional Programming are. I will delve more into them in future tutorials. It's very common for experienced programmers to not fully understand Functional Programming, so I expect to produce a lot of tutorials on that end of things.
Developing Scala Apps
If you already have a favorite IDE, its very likely there is a Scala plug-in for the IDE already being maintained by someone somewhere. Just google it. There are many other ways to develop Scala apps that just Eclipse. You can actually write Scala apps from inside Notepad and then compile them from the command prompt if you wanted to. However, Eclipse is one of the most common IDE's for developing Scala, so I'll stick with that much of the time.
Finding Scala Documentation
The API docs for Scala are at http://www.scala-lang.org/api/current/#package. You might notice they look a lot like Java docs. Considering Scala's long relationship with Java, that shouldn't me too surprising.
You can find more documentation for Scala at http://www.scala-lang.org/documentation/. At the documentation site, you'll find tutorials, a style guide, and some great cheat sheets.
Scala Hello World
- Open Eclipse and choose a location for your new workspace, if you haven't already.
- Inside Eclipse, create a new Scala project.
- Create a new Scala App inside your Scala project.
- Make the code match the hello world code below.
- Run your new Scala hello world app as a Scala Application from the Run menu
Here's the code for a basic Scala hello world.
If you know some Java, the first thing you might realize is that the code is directly entered into what Java programmers would think of as the class definition. If you've written many Java applications, you probably can't count the number of times you've written or had your IDE generate a main() method just so you could test a couple of lines of code.
Scala has a nice shortcut for that. Instead of writing main method, you can define a singleton object that extends App. That allows you to act as if you are writing code directly to your main() method with much less fuss and decoration that Java would allow you to get away with.
If you want to get a little more stylish with your Scala hello world, you can present the message in a dialog box with the following code.
Try It!Reading about a language is not how you learn it. Try the following activities to prove what you have learned about Scala.
- Try printing out a custom work of ascii art. There are plenty examples of ascii art on the web.
- Try creating and running a Scala application that draws your name using the letters of your name. So if your name was Bob, you'd draw a 'B' created from the letter 'B', an 'o' drawn from the letter 'o', and a 'b' drawn from the letter 'b'.
- Create a countdown from 10 to 0 using Dialogs that display one after another.