-
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
Set<T> should allow looking up an element #335
Comments
This comment was originally written by drfibonacci@google.com Added Triaged label. |
This comment was originally written by alexey.v.varla...@gmail.com Generally speaking, there is no point to look up an element in Set, plain check via contains() is idempotent. The use case you describe is rather filtering, i.e. you want to extract some element(s) from a Set, by characteristics which are orthogonal to equivalence semantics of a particular Set (e.g. to find an object by identity in a HashSet). There is a Collection.filter() feature already available to do this task: I would consider this issue as one more example of confusion caused by underspecified Set semantics (see issue #472). |
This comment was originally written by @seaneagan I think issue #3948 would be a nice solution to this. |
We are not planning to add methods that distinguishes otherwise equal objects, so looking up an object by an equal object should be equivalent to just returning the other object. Added NotPlanned label. |
There's is no way to look up an element in a Set<T>. I want to be able to write something like this:
class X implements Hashable {
int x;
X(this.x);
operator ==(other) => (other is X) && (x == other.x);
hashCode() => x;
}
final x0 = new X(42);
final x1 = new X(42);
assert(x0 == x1 && x0 !== x1);
final set = new Set<X>();
set.add(x0);
assert(set.contains(x0) && set.contains(x1));
assert(x0 === set.lookup(x0) && x0 === set.lookup(x1));
Not sure if lookup is the best method name, but getting access to the set element that tests equal to another given object is very useful.
The text was updated successfully, but these errors were encountered: