Skip to content

Dart 3 core library additions #49928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 tasks done
mit-mit opened this issue Sep 8, 2022 · 8 comments
Closed
6 tasks done

Dart 3 core library additions #49928

mit-mit opened this issue Sep 8, 2022 · 8 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core

Comments

@mit-mit
Copy link
Member

mit-mit commented Sep 8, 2022

Generally it's hard to evolve the Dart core libraries, as most changes tend to be potentially widely breaking. For example, adding a new member to a class in the core libraries can be breaking to any code that that implements that class, given all Dart classes are implicit interfaces.

While we still don't want major breaking changes in Dart 3 outside of the non-null discontinuation, we have an opportunity to be mildly breaking.

@mit-mit mit-mit added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core labels Sep 8, 2022
@Wdestroier
Copy link

Maybe add a name property to FileSystemEntity and change the firstWhere() method from Iterable to return a nullable value...

@mit-mit mit-mit changed the title Dart 3 core library changes Dart 3 core library additions Sep 27, 2022
@lrhn
Copy link
Member

lrhn commented Sep 27, 2022

With records being a thing, we may want to embrace them.

We have a number of "indexed" iterable extensions in package:collection that would otherwise be worthy candidates for inclusion in dart:core, but with records, we can instead introduce a single "add index to iterable" operation:

extension IterableExtension<T> on Iterable<T> {
  Iterable<(int, T)> get indexed sync* {
    var index = 0;
    for (var element in this) yield (index++, element);
  }
}

Then you would be able to do:

for (var (i, e) in something.indexed) {
  print("#$i: $e");
}

(We'll be missing some affordances in the first release, for example we probably won't have patterns in parameters, so not everything will be as smooth as the for-in.)

We should consider whether there are other uses of records that afford a similar big result for a small effort.

@eernstg
Copy link
Member

eernstg commented Sep 27, 2022

Usually this one is mentioned as well:

extension MapExtension<K, V> on Map<K, V> {
  Iterable<(K, V)> get keyed sync* {    
    for (var element in this.entries) yield (element.key, element.value);
  }
}

// Example usage.

void main() {
  var map = {1: "one", 2: "two"};
  for (var (key, value) in map.keyed) {
    print('$key: $value');
  }
}

@lrhn
Copy link
Member

lrhn commented Sep 27, 2022

The Map.keyed here is mostly redundant with the existing Iterable<MapEntry<K,V>> get entries.

You can do for (var MapEntry(:key, :value) in map.entries) ... with pattern matching. If we can get rid of the redundant MapEntry too, it would be even better.

(It would be nice to make MapEntry an alias for ({K key, V value}), but that would break code currently using the constructor. I'm more inclined to make it either a struct, or make it a view on ({K key, V value}), preferently a transparent view which can be cast in either direction, if such a thing exists. Really, I just want a place to hang the constructor.)

@spydon
Copy link
Contributor

spydon commented Oct 20, 2022

The DateTime.copyWith extension is now merged:
https://dart-review.googlesource.com/c/sdk/+/258541

@bernaferrari
Copy link
Contributor

If I could ask for anything crazy in Dart 3, I would go with print("value is", a). I alwaaays struggle with Dart syntax, and it is possible to implement that without varargs. Since print is not supposed to run in release mode, the minimal performance impact is negligible.

#49084

@mit-mit
Copy link
Member Author

mit-mit commented Apr 10, 2023

Hey @lrhn can you please check the list at the top (which I updated) to see if it's complete for 3?

@lrhn
Copy link
Member

lrhn commented Apr 12, 2023

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core
Projects
None yet
Development

No branches or pull requests

6 participants