Saturday, May 21, 2016

Getting Started with Scala

So you want to learn Scala. I will teach Scala to you from start to finish, but I do assume you at least understand some basics, like what is a variable; what is a function; and what does compile mean.

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?


You can write any type of application with Scala. If you want to write a traditional desktop app, you can write it in Scala. If you want to write scripts and command line tools, you can do it in Scala. If you want to write web services, you can do it in Scala. If you want to write JavaScript code for your website, you can do it in Scala. Scala is highly versatile and works for nearly any solution you need an application written for.

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


First thing's first. You need an environment to program Scala from. I use Eclipse. It works on pretty much any OS, so it makes my life easy when switching from OS X to Windows or even Linux. You can pull a prepackaged Eclipse with Scala tools from scala-ide.org. You can also get the information needed there to install the Scala tools into your existing Eclipse from that site.

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 main website for Scala is scala-lang.org.

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


No first programming tutorial is complete without a Hello World, and so let's finish off this Scala introduction with a brief Scala hello world example.


  1. Open Eclipse and choose a location for your new workspace, if you haven't already.
  2. Inside Eclipse, create a new Scala project.
  3. Create a new Scala App inside your Scala project.
  4. Make the code match the hello world code below.
  5. 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.


object HelloWorld extends App {
  println("Hello Scala 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.


import javax.swing.JOptionPane

object HelloWorld extends App {
  JOptionPane.showMessageDialog(
                       null, 
                       "Hello Scala World!!!")
}


Try It!

Reading about a language is not how you learn it. Try the following activities to prove what you have learned about Scala.


  1. Try printing out a custom work of ascii art. There are plenty examples of ascii art on the web.
  2. 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'.
  3. Create a countdown from 10 to 0 using Dialogs that display one after another.


No comments:

Post a Comment