Skip to content
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

List.unmodifiable should not accept an iterable of any type #52804

Open
navaronbracke opened this issue Jun 28, 2023 · 2 comments
Open

List.unmodifiable should not accept an iterable of any type #52804

navaronbracke opened this issue Jun 28, 2023 · 2 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. enhancement-breaking-change An enhancement which is breaking. library-core type-enhancement A request for a change that isn't a bug

Comments

@navaronbracke
Copy link

Recently I bumped into a bug where some code was using a lot of List.unmodifiable(someIterable) to assign an unmodifiable list to a List<T>. However, since List.unmodifiable accepts an Iterable<dynamic> a type bug slipped through.

This bug would have been prevented if the signature of List.unmodifiable was List<T> List.unmodifiable(Iterable<T> input).

Is there a specific reason why this type argument is the way it is?

Dart SDK version: 3.0.5 (stable) (Mon Jun 12 18:31:49 2023 +0000) on "macos_x64"

@vsmenon vsmenon added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label Jun 28, 2023
@vsmenon
Copy link
Member

vsmenon commented Jun 28, 2023

I believe this is intended to allow you to tighten the type based on what you know about the elements. E.g.,:

  var list1 = json.decode('[1, 2, 3]');  // List<dynamic>
  var list2 = List<int>.unmodifiable(list1);  // Triggers runtime checks to tighten to List<int>

@lrhn
Copy link
Member

lrhn commented Jul 2, 2023

That was the intuition. It was still a bad idea, born out of the unsafely type Dart 1, and that badness is the reason we introduced List.of as a better alternative to List.from.

We could either introduce an List.unmodifiableOf with better typing, risk a breaking change, or (my preference, even if it might have a much longer time frame) try to introduce unmodifiable list literals, say u#[v1, ... vs, v3].

@lrhn lrhn added type-enhancement A request for a change that isn't a bug enhancement-breaking-change An enhancement which is breaking. library-core labels May 14, 2024
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. enhancement-breaking-change An enhancement which is breaking. library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants