Skip to content

Immutable, Canonical, Equatable #15

@darwingr

Description

@darwingr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions