Data equality & toString

A modddel is always immutable, and it correctly overrides operator == and hashCode for data equality. Two modddels are equal if they are instances of the same union-case, hold the same properties, and hold the same failures if it's an invalid-step union-case. Internally, the modddel uses equatable for data equality.

The toString method is also overridden, and lists all the properties of the modddel ± its failures.

// In this example, `Age` is a ValueObject that is invalid if the age 
// is < 18, with as a failure `AgeLegalFailure.minor`

final age1 = Age(20);
final age2 = Age(10);
final age3 = Age(10);

print(age1);
// ValidAge(value: 20)

print(age2);
// InvalidAgeValue(failures: [AgeLegalFailure.minor()], value: 10)

print(age1 == age2);
// false

print(age2 == age3);
// true

Last updated