google / built_collection.dart

Immutable Dart collections via the builder pattern.
https://pub.dev/packages/built_collection
BSD 3-Clause "New" or "Revised" License
275 stars 52 forks source link

making all collections const #282

Open ahmednfwela opened 1 year ago

ahmednfwela commented 1 year ago

fixes https://github.com/google/built_collection.dart/issues/164

This PR adds a const factory BuiltList.fromList([List<E> list]) which creates a new subclass _ConstBuiltList that only has a const constructor.

a similar approach is also used to add support for const BuiltSet and other built collections.

this is a non-breaking change, since classes most likely extend BuiltList, and even if they implement it, they will just get a warning override_on_non_overriding_member lint.

However, this is however a behavioral change for people extending BuiltList themselves, since the hashCode operation is now slower, which means they will have to add back this piece of code themselves.

  int? _hashCode;

  @override
  int get hashCode {
    _hashCode ??= hashObjects(_list);
    return _hashCode!;
  }

P.S.: I have found 0 instances of people extending BuiltList on github.

cc @davidmorgan

ahmednfwela commented 1 year ago

We might want to change the name of the const constructor to something easier though, ideas are welcome!