Skip to content

Make new optional #20750

Closed
Closed
@Zectbumo

Description

@Zectbumo

I see how new makes it familiar to new programmers but the new keyword is really not necessary in the end. Dart has named constructors and static methods and it is up to the library maker to decide which is appropriate to use. Currently named constructors need `new' while static methods do not. My focus here is to relieve the programmer of having to remember what side of the bed the library developer woke up on and remembering do I need the new keyword or not.

Story time:
One day a library developer woke up and said parsing ints from strings is "discovering" the int so therefore it is not a constructor and a static method instead: Integer.parse(); was born. The library user MUST NOT use new to construct this Integer. Then another morning a library developer makes Element.html() and this time he think this is a constructor. The library user MUST use new to create his Element. This requires the library user to stop and think, do I need new or not?, when really in the end the library user just wants an object created; it doesn't matter how it's made.

Before:

var now = new DateTime.now();
var num = Integer.parse('54');
var elm = new Element.html(<u>54&lt;/u&gt;');

After (optional):

var now = DateTime.now();
var num = Integer.parse('54');
var elm = Element.html('<u>54</u>');

A discussion in issue #18241 talks about making const and new together being optional. This issue was created to focus on just making the new keyword optional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).customer-fluttertype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions