darwingr / xmoves

Quarantine inspired healthy activities app for Kennedy Nangle, 2020 micro-grant recipient from StFX's Frank McKenna Centre for Leadership
https://www.xmoves.ca
Other
0 stars 0 forks source link

Immutable, Canonical, Equatable #15

Open darwingr opened 3 years ago

darwingr commented 3 years ago

What is the relationship? Is there a common type knowable at compile-time between these?

Situation: The emit() function used by BLoC when transforming its State on Events does a comparison using ==.

Q1. Are instances of the same empty definition subtype that are neither Immutable, Canonical, Equatable to be considered equal?

No. You only get == comparison between instances for free when (1) a constant constructor is defined for the class and (2) both objects are instantiated as constants.

class Model {
  const Model();
}
final m = const Model();

That is canonicalized, which must be immutable.

Q2. I want to know the simplest approach of the 3 methods to take consistently in order to guarantee this works correctly.

If you want mutable or complex aggregate object state then simply implementing Equatable's props => []; method is very easy.

Otherwise Canonicalized object is less prone to errors and bugs.