BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Scala 3 Overhauls Language for Better Developer Experience

Scala 3 Overhauls Language for Better Developer Experience

This item in japanese

Bookmarks

Eight years in the making with 28,000 commits, 7,400 pull requests, and 4,100 closed issues, Scala 3.0 has been released, delivering a "complete overhaul of the Scala language" with new features, updates to the syntax and type systems, and new tools for metaprogramming. The Scala compatibility reference heralds version 3 as a "game changer [...] that will greatly improve the day-to-day experience of every Scala programmer". Scala 3 is binary backwards-compatible with Scala 2.13, but not fully source-compatible. The new Dotty compiler can automatically migrate old code and report any remaining issues.

Scala 3.0 is more concise with its new syntax. The new keyword is now optional (similar to Google having introduced this feature in Dart 2.0), as are parentheses in expressions of control structures. Some occurrences of curly braces may be replaced with source code indentation, a well-known feature of Python.

New language features allow developers to directly express their intent with opinionated contextual abstractions: using clauses and given instances simplify context parameters, extension methods are first-class language citizens, and implicit conversions are now built as instances of the scala.Conversion class.

The type system was improved with features such as simplified enums; opaque types that hide implementation details; intersection and union types to provide syntax such as A & B and A | B, respectively; new dependent function types whose result depends on a function's parameters; and built-in type lambdas that once required a compiler plugin in Scala 2.

The enum syntax in Scala 2 was verbose. Consider the following example:

sealed trait Direction 
case object East extends Direction
case object West extends Direction

Scala 3 introduces a more concise syntax:

enum Direction:
   case: East, West

Functional programming has been simplified with the redesign of the contextual abstractions and type system changes. Object-oriented development was also improved with new features such as: traits accepting parameters and being declared as transparent; an open modifier that explicitly marks classes as open for extension; and in the spirit of encouraging the Decorator design pattern principle, favor composition over inheritance, by providing export clauses that define aliases for selected members of an object.

Macros, first introduced in Scala 2.10, was an experimental feature. Scala 3 introduces a set of metaprogramming tools featuring: inlining methods to improve application performance; compile-time operations that may be used with inline methods; quoted code blocks to construct and analyze code; and a reflection API to provide a view of the typed abstract syntax trees including TASTy files. Designed for stability, Scala 3 macros will be compatible with future versions of Scala. However, with this new metaprogramming model, Scala 2.13 macros will require rewriting. In addition, many Scala 2 scaladoc settings have changed to standardize parameters in Scala 3.

There are new compiler options such as: -Xignore-scala2-macros will ignore compiler errors with code that call Scala 2 macros; -print-tasty will print raw TASTy files; and -no-indent will enforce the classical syntax. The migration guide details the move to Scala 3 and contains a tutorial on porting a complete Scala 2.13 project to Scala 3.

A Scala 3.0 application may still use a Scala 2.13 library even with macros — unless the non-working macros break the library. The opposite is also true, such that a Scala 2.13 application may use a Scala 3.0 library. The Scala Library Index, AKA Scaladex, lists which libraries are ready for Scala 3.0.

These changes come at a price, however, as some Scala 2.13 source code is incompatible with Scala 3.0. Fortunately, the new compiler includes a migration mode that can automatically migrate Scala 2 code to Scala 3 and report any remaining issues that require manual fixing.

More information on Scala 3 may be found on the what's new section of the website.

The easiest way to get started with Scala 3.0 is with Scastie, Scala’s online IDE, where code may be written and executed directly in the browser. The documentation also demonstrates how to run a "Hello, world" program.

Five weeks after the release, the Scala 3.0 documentation and book are incomplete, and the Scala community is encouraged to contribute.

New Scala releases are expected every six weeks after the 3.0 release. Developers may expect the next release at the end of June 2021.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT