My attempt at a “HelloWorld” tutorial with (hopefully) as few assumptions about your coding knowledge as possible.
I hope you find it useful 🙂
- I am using Scala version 2.12.5 (for help installing Scala click here)
- I am using sbt version 1.1.4 (for help installing sbt click here)
- I am using Sublime to write my code (available to download from here)
- I am using a Macbook, and therefore their OS X
- I am using the terminal to run my code
What is SBT?
sbt stands for Simple Build Tool and it is what most people use to build their Scala projects. It provides the project with the support it needs to compile the code and make it work.
Below is the way I have structured a Scala project that uses sbt. Most of it is necessary for sbt to find all of the things that it needs, however the structure inside
/src/test/scala is up to you 🙂 A lot of people put their
Main.scala straight in the
/src/main/scala directory and then organises the rest of the code in subdirectories.
What is in the
project directory and why?
As you can see in the the image above, sbt needs a
project directory that contains a
build.properties file. The content of this file looks like this:
This tells the sbt launcher what version of sbt to use.
What is the
build.sbt file and why?
sbt also needs a file called
build.sbt to live inside your root directory (you can see where this is in the image 1 above). This file tells sbt how to build the project, but for this HelloWorld example we don’t need it to do that much.
The above is the minimum that you need to put in the
name is just the name of your project, usually the name of the directory all of your code is contained within. It has to be a string (a string is a group of characters contained by a set of quotes) so here my project name is “blogPostProject”.
scalaVersion is where you define which Scala version you want to use. Most people use the most recent stable version, you can find this information here.
What is the
HelloWorld.scala is where we are writing our first bit of code!
But right now, it looks like this:
The package name I’ve used comes from the structure in image 1, each directory that comes after
/src/main/scala and before the file you’re writing in is separated by a
. and this forms the package. It’s worth mentioning that this is convention rather than by necessity so don’t worry about it too much, your package doesn’t have to mirror the directory structure (as long as you have
Let’s get our Scala project to write out “Hello, World!” when we run it.
How do we “run it”?
So to “run” the code, I usually open up my terminal, you may find yours on your Mac in the Utilities directory inside your Applications directory. To tell the terminal which directory to look at, I
cd from where I am –
~ is the root of what is called your home directory – to where my project is saved.
cd is a command line command that tells the terminal to switch to the directory you tell it to, click here to see more command line commands.
So, in this case we are using the command
cd Desktop/blogPostProject/ as I have saved my project called
blogPostProject on my
Once we are in the right place, we can use sbt to execute different tasks on our Scala project. To run our code, we type the command
sbt run in the terminal and sbt should do some magic!
OK so that doesn’t look too good. This is because our
HelloWorld.scala is empty and sbt doesn’t know what to run.
The above error in sbt is saying that there is
No main class detected.
Let’s create one. But first…
What is data?
Data is stuff our program can process
What is a function, and how do we make one?
A function is something you can define, that can take arguments, does something, and then returns something. They are instructions for different ways to process data.
To define a function we start of by using the keyword
def, this is then followed by the name you are giving the function. It’s common practise in Scala to give a function a name that begins with a lowercase letter and is in camel case format. The minimum you can do to create a function is then to write an equal sign
= and then write the code you want it to execute.
A very basic function looks like this:
This is a function that takes no arguments and returns an
Int is the type we use when defining an Integer).
To write a function that takes arguments, we use brackets that come immediately after the function name. We give those arguments names so that we can reference them in the code, and types so the function knows what to expect.
This function takes two arguments (also known as parameters), adds them together, and returns the result.
If we look at this function quickly, it’s not immediately obvious what type of thing this function returns. So, we can add a return type to the function (also described in more detail later in this post).
And finally, the above code
number1 + number2 is just one expression so we haven’t needed to use any curly braces. When we write something that has a few more stages, we need to use braces as below.
What is a return type?
A return type is defined by a colon after the initial function declaration. As you can see in image 10, the return type here is defined by
: Int. It tells the developers and the compiler what type it expects to return; the result from the code inside that function.
Note: you don’t always need to define it, but it’s fairly good practice to.
What is Unit?
Later on you will see a return type of Unit. Unit is a special kind of type used in Scala that represents only one value –
It is used when we can’t return something of value back from the function.
Later on, we will use the
println returns a Unit, as it is having a side effect in the terminal (printing a value) but we don’t have anything to return.
println is a function that is included in Scala as default. It is a function that accepts a parameter, and tries its best to display it wherever you are running the code, in this case the terminal.
What is a
class in Scala?
Classes are instructions for how to build objects, you’ll be able to use a class to create many instances that are called objects.
For example, if you have the class
IceCreamMaker, you can use this to create multiple objects for the different flavours of Ice Cream.
Don’t worry too much about the syntax for now though 🙂
What is an
object in Scala?
An object groups together data and functions that represent individual instances of the thing we’re modelling (e.g. an Ice Cream Maker), and gives them specific behaviour (e.g. a function to freeze the mixture).
So, if we create an object called
StrawberryIceCreamMaker, and it has a function called
freezeMixture then you can write
StrawberryIceCreamMaker.freezeMixure to execute the code.
So, how do we create that main class?
As we said above, an object is just one instance of a class. This allows us to use an object for the
main class rather than a class.
So let’s make an object, I can write the following in
This above code is defining an object called
What happens if we try
sbt run now?
It still fails for the same reason 😦 We have defined a class, or object in this case, however we still haven’t defined a main class.
For this object to work as a main class, we can do one of two things
- Add a
For sbt to recognise this as a main class, you may add a function to the object that satisfies all of the following:
- function must be called
- function must have a return type of
- function must accept one argument that has the type
2. Object must extend something called
Don’t worry too much about the
extends here, this probably deserves another blog post, but basically
extends lets you inherit functions from the thing you’re extending. Here,
HelloWorld is inheriting the
main function from
main function it is inheriting also satisfies all of the criteria from the list above, therefore it works!
Lets run it!
So, my personal preference when creating a main class is to use the
extends App option, so now I have this:
and I’m going to use the
sbt run command in the terminal again.
And as you can see just above the green success word, that we have printed out the words