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

Extend List with method exists(int index) #46400

Open
chreck opened this issue Jun 18, 2021 · 1 comment
Open

Extend List with method exists(int index) #46400

chreck opened this issue Jun 18, 2021 · 1 comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug

Comments

@chreck
Copy link

chreck commented Jun 18, 2021

It is inconvenient to check if the list has an item on this index or not. The only way to solve this, is to add an extension on List implementation.

extension ListExtension on List {  
  bool exists(int index) {
    return index >= 0 && index < this.length;
  }
}

void main() {
  var list = <String>[];
  list.add("First");
  var index = 1;
  if(list.exists(index)) {
   list[index];
  }
  else
  {
    print('Index $index does not exist in list $list');
  }
} 

Otherwise an exception is immediately thrown.

Uncaught Error: RangeError (index): Index out of range: index should be less than 1: 1

@lrhn lrhn transferred this issue from dart-lang/language Jun 18, 2021
@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug labels Jun 18, 2021
@lrhn
Copy link
Member

lrhn commented Jun 18, 2021

We can't add methods to the List interface, it would break too many third-party implementations of the interface.

Adding an extension method is possible, but I don't think this particular method is important enough that it needs to belong in dart:core or dart:collection. If anything, it would be package:collection.

It's not a method I think I'd personally ever use.
If I have an integer, and I don't know whether it's less than zero, I'm very, very unlikely to want to use it as a list index.
The only case where that's likely to happen is if someone else passes me an integer as argument, and in that case I'll just require the index argument to be valid for the list. Probably using:

RangeError.checkValidIndex(index, list, "index", list.length);

If the number is known to be non-negative, using this method is harder, and no more readable, than just doing index < list.length.

Others might have use-cases where it makes more sense, but I personally don't.

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 type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants