dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.09k stars 1.56k forks source link

DateTime constructor should have an optional, named utc parameter #47063

Open jamesderlin opened 3 years ago

jamesderlin commented 3 years ago

It's quite annoying that DateTime has separate constructors for non-UTC and UTC DateTimes instead of offering a single constructor with a boolean flag. This makes attempts to implement copyWith-like methods (see https://github.com/dart-lang/sdk/issues/24644) rather tedious and error-prone since they must invoke different constructors based on isUtc and duplicate all of the arguments.

Of course, this wouldn't be a problem if:

lrhn commented 3 years ago

That's not currently possible. The DateTime constructor has optional positional parameters, so it cannot also have a named parameter.

Now, if we allowed both (https://github.com/dart-lang/language/issues/1076), that would probably have been the design all along. If we add that ability, I promise I'll personally put a {bool utc = false} parameter on the DateTime constructor. (My current desired approach is to remove the notion of declaring optional parameters entirely, and make any nullable parameter optional.)

There will be constructor tear-offs.

spydon commented 2 years ago

There are now constructor tear-offs, so that can be used as a work around, meanwhile we eagerly wait for https://github.com/dart-lang/language/issues/1076