You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of Dart 2, Effective Dart suggests not using the new keyword and removing it from existing code.
Doing that makes named constructors and static methods look the same at call sites.
Declare a constructor by creating a function with the same name as its class [...]
Thus suggesting that a constructor is a function. But it turns out it is not really a function, as it cannot be used in all contexts where a function can.
Why it would be useful
Dart built in types, in particular collection types, have several methods that take functions as arguments, for instance .forEach and .map, two concise and expressive ways to perform computations.
This feature request intends to allow using constructors where functions with the same type/signature are expected.
Example code: https://dartpad.dartlang.org/9745b0f73157959a1c82a66ddf8fdba4
Background
As of Dart 2, Effective Dart suggests not using the
new
keyword and removing it from existing code.Doing that makes named constructors and static methods look the same at call sites.
The Language Tour says (emphasis mine):
Thus suggesting that a constructor is a function. But it turns out it is not really a function, as it cannot be used in all contexts where a function can.
Why it would be useful
Dart built in types, in particular collection types, have several methods that take functions as arguments, for instance
.forEach
and.map
, two concise and expressive ways to perform computations.In an program I'm working on, I got a bit surprised by not being able to create new instances using
Iterable.map
+ a named constructor. And then I realized that others have arrived at the same conclusion at least 4 years back on StackOverflow, but I could not find a matching issue.Example Flutter code
What I would like to write:
What I have to write instead with Dart 2.1.0:
Note that the feature request is not specific to Flutter code, but applies to any Dart code as per the more generic example in https://dartpad.dartlang.org/9745b0f73157959a1c82a66ddf8fdba4.
dart --version
)The text was updated successfully, but these errors were encountered: