dart-lang / macros

A Dart mono-repo for macro development.
BSD 3-Clause "New" or "Revised" License
65 stars 5 forks source link

Extension type `operator==` will mostly not work #65

Open davidmorgan opened 2 months ago

davidmorgan commented 2 months ago

Since the JSON types are extension types over Map, operator== will mostly not compare by value and will fail.

In https://github.com/dart-lang/macros/pull/60 I add an equals method that does work.

This is surprising and likely to trip people up.

Brainstorming:

Should we have a lint "don't use operator== on extension types"? If so it should presumably also catch them being used in Sets or as Map keys.

Another thing we could do, since we usually control the underlying types, would be to make those implementations (e.g. _TypedMap, _GrowableMap) have deep equality / hash code. But that would be confusing if people use the fromJson contructors with standard maps.

eernstg commented 2 months ago

Should we have a lint "don't use operator== on extension types"? If so it should presumably also catch them being used in Sets or as Map keys.

Makes sense! .. but we might add another requirement: Warn against using operator ==

For example, we might easily encounter situations where the representation object uses the operator == from Object (at least in the statically known type, and "as far as we know" in subtypes as well), and the same kind of equality is a good match for the extension type.

Perhaps the lint could detect metadata like @equatable extension type ....? Or perhaps it's better to assume that == is OK by default, and then have @noEqualsEquals extension type .... on extension types where it isn't OK?