collection icon indicating copy to clipboard operation
collection copied to clipboard

Add an option (possibly the default?) for DeepCollectionEquality to handle self-referential structures

Open DartBot opened this issue 10 years ago • 6 comments

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="96" height="96"hspace="10"> Issue by nex3 Originally opened as dart-lang/sdk#18729


Sometimes it's useful to be able to compare nested object structures that can contain self-referential loops. Currently DeepCollectionEquality causes a stack overflow when passed such structures, but it would be nice if it could handle them. This could be done either as a configuration option or as the default if the prospect of storing back-references isn't too worrisome.

Currently the YAML package has its own custom implementation of deep equality, but it would be nice if it could use the standard implementation instead.

DartBot avatar Jun 04 '15 00:06 DartBot

<img src="https://avatars.githubusercontent.com/u/4865287?v=3" align="left" width="48" height="48"hspace="10"> Comment by lrhn


That should be doable. For each operand, remember each object it is currently being compared to, and if you see that combination again, return true. It's not fool-proof, but it will catch most simple self-referential structures.

DartBot avatar Jun 04 '15 00:06 DartBot

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> Comment by nex3


I believe YAML's deepEquals operation works like that, but it remembers the order of the objects it's being compared to as well.

DartBot avatar Jun 04 '15 00:06 DartBot

<img src="https://avatars.githubusercontent.com/u/17034?v=3" align="left" width="48" height="48"hspace="10"> Comment by kevmoo


Aligning w/ the bits offered from matcher would be nice, too.

Fewer implementations FTW

DartBot avatar Jun 04 '15 00:06 DartBot

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> Comment by nex3


Matcher's use case is a little different, because it needs to track a bunch of extra information to provide useful messaging when the equality fails.

DartBot avatar Jun 04 '15 00:06 DartBot

This has been open since 2015 without action so I am closing as stale.

matanlurey avatar Mar 12 '18 16:03 matanlurey

@matanlurey I appreciate your zeal for cleaning up the issue tracker, but it's useful for me as a maintainer of this package to track features that would be useful to users, even if I don't often get a chance to work on those features. As long as these features would still be useful, I'd like to keep the tracking issues open.

nex3 avatar Mar 12 '18 23:03 nex3

I don't want to make the existing deep equality check for cycles. That's a lot of work (and memory for caching) which has no effect in most cases.

We can add another equivalence which does handle cycles, if we want to.

lrhn avatar Sep 09 '22 11:09 lrhn