Description
This issue was originally filed by bord...@gmail.com
Dart should provide syntatic sugar for defining value objects, automatically defining the == operator and hashCode property based upon class member variables. The common pattern for defining such an object is tedious and error prone. For instance:
class ValueObject {
final dynamic prop1;
final dynamic prop2;
//.etc
final int hashCode;
ValueObject(var prop1, var prop2) :
this.prop1 = prop1,
this.prop2 = prop2,
this.hashCode = generateHashCode([prop1, prop2]); // Similar to guava's Objects.hashcode()
bool operator==(other){
if (identical(this, other)) {
return true;
} else if (other is ValueObject) {
ValueObject that = other;
return (this.prop1 == that.prop1) && (this.prop2 == that.prop2);
} else {
return false;
}
}
}
This pattern requires a diligent developer to write a complete set of tests to verify they've implemented both hashCode and == correctly. Instead dart should provided syntax for defining such classes and automatically implement the == and hashCode methods during compilation.