In the earlier video, I mentioned that SBT is designed to support an experiment driven
development approach. In this method,
you test different things in an interactive environment, and once you are happy with the
results,
you persist your code in a permanent source code file. We learned to use that approach with
sbt and create your build definition file. In this video, I will introduce you to
Scala,
and we will learn to use the experiment driven development in Scala programming.
Start SBT console. You are already familiar with
sbt. The
sbt console is an
sbt task. It will start Scala REPL for you. The Scala REPL is an interactive Scala
shell,
or you can think it as Scala interpreter. Every line that we enter on the Scala prompt is
interpreted
and executed by Scala REPL. It also displays the outcome and imports the result into the
current
scope. Execute the below command.
Above code defines a Scala variable. After executing above code on REPL, you will see some
output. It shows that
h is a string and its value is
Hello Scala. The outcome of that declaration, I mean the value of the variable is
automatically
imported into the current scope. So, if I print h, it will be a valid statement.
The Scala REPL won't complain that
h is undefined. The
sbt console takes this idea to the next level and imports your project classes into
the
current scope. Let me show you.
Let's create a Test class in your project directory. Create a file named
Test.scala and place below content in the file.
Now, if you start the console from your project directory, Scala will compile your project and import this class into the current scope. Let's try it.
The above code works. That means, the class
Test is in the scope. So, you experiment with the code on REPL, once you are happy
with
the code, place it into your source code file. Restart your console, and you are ready to
experiment
further. This allows you to also use the code that you have finalized and placed it in one of
the
source files in your project. We will be using this approach throughout the tutorial.
Let me show you one more thing. I want to import one of the Spark packages. Let me try.
You should get an error. It looks like Scala REPL doesn't know the above package. I can exit the console and define the dependency at the SBT prompt.
Restart the console and try to import Spark package once again. It should work now. You can
move that dependency to your
build definition file. Next time when you start SBT console, it should automatically add it to
the
classpath.
The point that I am trying to make is that, whenever you start your sbt console in a
project
scope, sbt will compile your project and bring all the source code and dependency to the
current
scope. Such a behaviour makes our life simple and supports the experiment driven development.
If
you are working on a data engineering or a data science project, you need this kind of support
because
it's going to be an experiment driven development for sure.
We are now ready to start learning Scala. We will start with some basics of Scala
programming
from the next video.
Thank you for watching Learning Journal.
Keep learning and Keep growing.