Its first major breaking change

Dart 2.0 is coming: Strong mode becomes the standard type system for Dart

Gabriela Motroc
Dart 2.0

© Shutterstock /ESB Professional

Once Dart 2.0 is here, Strong Mode will simply become the standard type system but for now, let’s rejoice over the idea of a stronger Dart. Read on if you want to find out how the phases of moving to 2.0 look like.

Less than two weeks after the Dart team announced their intention to make strong mode – a sound static type system for Dart – the standard, David Morgan, software engineer at Google revealed that when the highly-anticipated Dart 2.0 arrives, strong mode will just be “the type system”.

What is Strong Mode?

“Strong mode is a sound static type system which uses a combination of static and runtime checks to ensure your code is type safe—that you can never see a value whose runtime type does not match its static type,” according to the FAQ page. “With strong mode enabled (in an implementation that has both the static and runtime checks), Dart is a type safe language ensuring that static type annotations are actually correct at runtime.”

One of the benefits of strong mode is that it helps you find more bugs at compile time, rather than runtime. Furthermore, refactoring tools and code completion work better with this type system. Since a lot of users have already embraced it, the team has decided to make it the standard type system for Dart.

The move to Dart 2.0 will have three phases:

1) strong mode is opt-in

2) strong mode is the default (you’ll be able to opt out)

3) strong mode is the only mode.

Moving to Dart 2.0: Phases

The move to strong mode also enables the use of dartdevc, the new Dart development compiler (DDC) instead of Dartium for web app development. One of the benefits of DDC is that “you get faster developer cycles and can use any recent version of Chrome – unlocking among other things the use of bleeding edge browser features.” According to the blog post announcing the strong mode, once the developer experience with DDC matches that of Dartium, the latter will no longer be maintained.

SEE ALSO: Flutter: A beautiful mix of React and Dart for mobile SDK

The joys of Dart 2.0

According to Morgan, “extra static checks via the analyzer help catch bugs, but they’re not enough to close all the holes. What’s needed is some additional runtime checks.” DDC is currently the only Dart runtime that implements those checks.

Dart 1 code:

void main() {
  cleanUp([new TempFile(), new BankAccount()]);
void cleanUp(List<TempFile> files) =>
    files.forEach((f) => f.delete());
class TempFile {
  void delete() => print('TempFile deleted.');
class BankAccount {
  void delete() => print('BankAccount deleted. Whoops!');
> TempFile deleted.
> BankAccount deleted. Whoops!

“TempFile” and “BankAccount” have a “delete” method. Morgan explained that since Dart 1 does not, by default, care about types, it will allow us to call “delete” on a “BankAccount” when we were very clearly trying to call “delete” on a “TempFile”.

If we run this snippet with DDC we get a runtime error:

Type 'BankAccount' is not a subtype of type 'TempFile'

Our bank account is safe. One of the benefits of Strong Mode with runtime checks is that if we try to run “TempFile.delete” we will never run “BankAccount.delete” instead.

There’s a reason why DDC is the only runtime to add these checks: they are breaking changes to the language. Programs that used to be valid are no longer valid. Since it’s a development tool, it doesn’t need to compile and run all valid Dart; the VM and dart2js do. With the change to 2.0, programs with type problems can be declared invalid — and all the runtimes can move on to providing types that do what they promise, Morgan added.

The glass is not always half full

What if we want loose types from Dart? Morgan explained that users can opt out of type checks with the special “dynamic” type. In fact, “dynamic” is the default if you don’t add a type annotation, so we could just have written “List” instead of “List<TempFile>”:

void main() {
  var list = new List<TempFile>.from(
      [new TempFile(), new BankAccount()]);
> TempFile deleted.
> BankAccount deleted. Whoops!

The “cleanUp” method is now written to work on any class with a “delete” method .

Dart 2.0 is coming. Why should you be excited about it? Because it’s the language’s first major breaking change and, according to Morgan, “it’s precisely because it’s a breaking change that it’s such an important development.”

Stay tuned!

Gabriela Motroc
Gabriela Motroc is an online editor for Before working at S&S Media she studied International Communication Management at The Hague University of Applied Sciences.

comments powered by Disqus