Skip to main content

New site for Dart news and articles

For the latest Dart news, visit our new blog at  https://medium.com/dartlang .

Breaking Change: The RegExp constructor will not use const

The RegExp constructor will have its const keyword removed today.

Anders Johnsen writes:

The reason for this change is that const classes can only initialize their fields at construction. However a RegExp needs to check the given pattern for syntax errors and throw at construction.

A few tips for converting old patterns:

class A {
  const RegExp myRegExp = const RegExp(...);
}

Should now be written as

class A {
  static final RegExp myRegExp = new RegExp(...);
}

In extend to that, consider moving out 'const RegExp(...)' from inside function bodies, if it's in a critical path, and place it either as a static final class field or as a top-level final variable.

As always, we invite you to join our Dart mailing list, ask questions on Stack Overflow, or file feature requests on dartbug.com.