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.
_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
We have no current plans to change the List API. It's a very central API, and adding a method to it will break all classes currently implementing List. The benefit has to be very great for us to do that (no matter how convenient such a method would be).
We have no current plans to change the
ListAPI. It's a very central API, and adding a method to it will break all classes currently implementingList. The benefit has to be _very great_ for us to do that (no matter how convenient such a method would be).
can it be done with extensions so it don't break implementations?
I know there may be a package that does this, but I have the feeling that this kind of stuff should be in the core
You can definitely write an extension, but then ObservableList won't be able to intercept the operation (as suggested above) unless the target is statically typed as ObservableList.
(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 swap in the platform libraries, because anyone can add it just as efficiently).
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;
}
}
}
Most helpful comment
+1, would be nice to have this method in the core SDK.