5

A Beginner's Guide to Scala 3.0 - Knoldus Blogs

 2 years ago
source link: https://blog.knoldus.com/a-beginner-guide-to-scala-3-0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
A Beginner's Guide to Scala 3.0
Knoldus Blog Audio
Reading Time: 6 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.

Knoldus prepares a playlist on Scala 3 explaining the new features of Scala 3, aka Dotty. Learn a lot about Scala 3 through these videos.

Explore how 3TL controls the high traffic spikes and facilitates communication in a non-blocking environment.

Technologies Used: Angular 4 Typescript, Nginx, Scala, Akka, Play Framework, Spray, Elasticsearch

Explore More

If you are looking to migrate to Scala 3/ Dotty and don’t know where to start, shoot us a message. Knoldus has been working on Scala, Akka, and Play since 2011. We love its support for functional programming and reactive architecture. Years of experience in the Scala programming language means we can support your business in any of your Scala-related projects. We support the end-to-end software development cycle and let you focus on the vision and business aspects of the product.

References:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK