A Beginner’s Guide to Scala 3.0

Reading Time: 5 minutes

Dotty, a comprehensive name for all the things that are being added to Scala 3.0 has been a topic of discussion in the Scala community for the last four years. With all the promises and progress, The time for the release is very near. We can expect a release by the end of the year. In this post, we will get a teaser of what changes to expect from Dotty as well as some of the new changes in the major release. At the end of the post, we will see how we can quickly start with the Dotty compiler and example code.

Screenshot from 2020-06-04 08-41-20

What changes to expect from Dotty?

If you would ask, “Is Scala 3.0 a new language”, Martin Ordersky says “yes Or no” in “A tour of Dotty.”  He has reasons for it. If you ask me, I would say yes!! It has a lot of new features.

  • If you are a beginner, and only starting with Scala 3, you will find the new features as a new concept that was not part of the old Scala.
  • Below snippet is a poll depicting the favorite changes for beginners in Scala 3.0.  which was shared by Martin Ordersky in the last edition of ScalaDays.
Screenshot from 2020-06-04 08-48-09
  • However, it does not end here. There are more changes which you will see in the next section.
  • If you are at an intermediate level in scala and already familiar with the constructs of the language, you will see some of the features which are removed and no longer supported. We will see those in detail in the upcoming sections.
  • If you are a Scala expert and FP Enthusiast, there is a list of changes for you as well such as, type-level derivation, match types, implicit function types, Metaprogramming to name a few.
  • These changes are shared in “A tour of Dotty”  by Martin Ordersky in the last edition of the ScalaDays.

Breaking Changes!

What do we mean by breaking changes? Those changes which are not going to be supported and compiling them in the newer version will require a re-write of the source in either a new way or to be removed. There are some features that are considered to have been pain points performance-wise or creating unnecessary complexity that is going to removed from Scala 3.0.

At a preview level here is the list of breaking changes:

Macro:  One of the biggest breaking change is Macros. The newer version of Scala includes a newer macros-system and Experimental Macro from Scala version 2.13 will not work in 3.0 and will have to be re-written.

22-Arity: The limits of 22 for the maximal number of parameters of function types and the maximal number of fields in tuple types have been dropped.

And many more features. Here is the list of all the breaking changes you should go through if you think you will be porting your code at some point in time.

An exhaustive list of Dropped Features is given below: 

There is a lot of new features to Gulp!

If you visit the official dotty documentation, you will see a big list of changes to cover in terms of feature which are being included in Scala 3.0. Here is a Snippet highlight:

Screenshot from 2020-06-05 10-51-52

May be as part of upcoming blogs, we will talk about some of the important features to start with but now without talking about any of the above. I encourage you to find the complete changes and the new language constructs, you can go through the official dotty documentation. As part of this blog, we will only be going through a glimpse of the features.

QuickStart with Dotty

There multiple ways to start with dotty.  It depends on how fast you want to try the new Scala 3.0

Dotty REPL

If you want to eagerly evaluate any expression in Scala 3.0, you can try it with REPL. REPL comes pre-packed with Dotty binary which can be downloaded from the latest releases.

Extract the downloaded binary and open bin/dotr in the terminal (I hope you are on Linux if not, you know what to do!). After starting REPL successfully, you can begin experimenting with your favorite topic. I’ve created an Enum here.

Screenshot from 2020-06-06 07-23-17

The Gitter8 Template

You can always create a sample project using the g8 template provided by dotty on the dotty documentation. The simple sbt new will give a Scala project template that uses the Dotty compiler to compile the program. The source code looks nothing different than plain old Scala source. Running the template gives a message which you can see in the screenshot.

Screenshot from 2020-06-06 13-27-14

What is different in the build?

When you will unpack the template source, you will see the build.sbt which specifies the dottyVersion. 

Screenshot from 2020-06-06 13-37-20

At the time of writing this, as per the sbt-dotty plugin, any version which starts with 0 will be considered as dotty and will be compiled using the Dotty compiler. The Dotty Plugin is required. You can include the same with the below plugin definition as part of your project/plugins.sbt file.

addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.1")

Show me the Code!

Now we are talking Code! Below is the code that I downloaded which had nothing new in terms of Scala 3.0. apart from the build definition.

Screenshot from 2020-06-08 04-19-01

Show me the new Code!

To get a new feel of the language, Here is how I converted this piece into something similar.

Screenshot from 2020-06-08 04-46-54

It looks like python. Isn’t It? Scala 3 offers a new way to define programs that can be invoked from the command line: A <strong>@main</strong> annotation on a method turns this method into an executable program. You can find out more about it here.

IDE support for Dotty.

On the official website of Dotty, it only mentions the official IDE support for Dotty is Visual Studio. I do not know the reason why my favorite IDE is not yet supported officially but there is a workaround. The workaround is described in latest blog on “Dotty support on Intellij”.

I hope you got a glimpse of Dotty. I will see you in the next blog with some feature-wise details. Thanks for reading!! Stay Tuned!!

References:

Written by 

Manish Mishra is a Sr. Software Consultant, with experience of more than 7 years. His primary development technology was Java. He fell for Scala language and found it innovative and interesting language and fun to code with. He has also co-authored a journal paper titled: Economy Driven Real Time Deadline Based Scheduling. His interests include: learning cloud computing products and technologies, algorithm designing. He finds Books and literature as favorite companions in solitude. He likes stories, Spiritual Fictions and Time Traveling fictions as his favorites.

1 thought on “A Beginner’s Guide to Scala 3.07 min read

Comments are closed.