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

Set<T> should allow looking up an element #335

Closed
kasperl opened this issue Nov 4, 2011 · 4 comments
Closed

Set<T> should allow looking up an element #335

kasperl opened this issue Nov 4, 2011 · 4 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-not-planned Closed as we don't intend to take action on the reported issue

Comments

@kasperl
Copy link

kasperl commented Nov 4, 2011

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.

@DartBot
Copy link

DartBot commented Nov 4, 2011

This comment was originally written by [email protected]


Added Triaged label.

@DartBot
Copy link

DartBot commented Nov 16, 2011

This comment was originally written by [email protected]


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:
assert(!set.filter(bool(el)=> el === x0;).isEmpty());

I would consider this issue as one more example of confusion caused by underspecified Set semantics (see issue #472).

@DartBot
Copy link

DartBot commented Jul 1, 2012

This comment was originally written by @seaneagan


I think issue #3948 would be a nice solution to this.

@lrhn
Copy link
Member

lrhn commented Mar 15, 2013

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.
If you really care about which instance of equal objects you have in your set, either use a Map instead (with same object as both key and value), or use set.firstWhere((x) => x == other), which is going to be slower, but will get you the object.


Added NotPlanned label.

This issue was closed.
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. closed-not-planned Closed as we don't intend to take action on the reported issue
Projects
None yet
Development

No branches or pull requests

3 participants