-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 swap method #22306
Comments
Added Area-Library, Triaged labels. |
Especially this will be useful with ObservableList, to get only one change record, with added and removed fields simultaneously. |
+1, would be nice to have this method in the core SDK. |
+1 |
1 similar comment
+1 |
We have no current plans to change the |
can it be done with extensions so it don't break implementations? |
You can definitely write an extension, but then (Again, interface default methods would allow us to add members to an interface without (necessarily) breaking classes implementing the interface. We can break classes which already uses the same name, but that's still less than breaking all classes implementing an interface. Until we get interface default methods, I don't see a big urge to add extension ListSwap<T> on List<T> {
void swap(int index1, int index2) {
var length = this.length;
RangeError.checkValidIndex(index1, this, "index1", length);
RangeError.checkValidIndex(index2, this, "index2", length);
if (index1 != index2) {
var tmp1 = this[index1];
this[index1] = this[index2];
this[index2] = tmp1;
}
}
} |
Are "interface default methods" being considered or tracked in any issue? |
dart-lang/language#884 outlines a proposal for interface default methods. There's a different proposal with similar properties in dart-lang/language#2510: class extension members. Interface default members are real instance members, which means that if a class Class extension members are more like extension members. This means that they are resolved statically, and there's no way you could write a declaration which will dynamically override the statically known declaration for any given call site. The other side of that coin is that interface default member conflicts are more difficult to handle: If |
Conflicts between interface default methods are like conflicts between any other interface methods, if a subclass can't define a member signature that satisfies both, it cannot implement both interfaces. Interface default methods exist to solve the software engineering problem of adding members to interfaces that are already implemented by third-party classes, without those classes suddenly not satisfying the interface. It's just about providing a default implementation of new members, it can't solve API conflicts. |
I would like to propose a 'swap' method on List, which would have the following functionality:
void swap(int x, int y) {
var tmp = this[x];
this[x] = this[y];
this[y] = tmp;
}
Of course there would probably be some range checks, but this would greatly enhance the List class for me, and I'm sure others.
The text was updated successfully, but these errors were encountered: