-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple syntax for defining value objects #10551
Comments
Set owner to @gbracha. |
This comment was originally written by bordol...@gmail.com I think that this issue is related to 501 but not strictly a dupe. 501 is seeking to add strong guarantees of deep immutability, which while desirable, seems like it may be complicated to implement in dart. With this RFE, I'm looking for an easier way to implement equality reliably and correctly, assuming the developer is not actively trying to subvert the equals contract by using mutable member variables. |
This comment was originally written by tobias.r...@gmail.com Another way to simplify implementation of value types is to add a syntax to generate methods such as get hashCode, equals, compare, toString, ... Just as constructors can be generated using the Contstuctor(this.s, this.b) syntax. Some annotations would cover most cases. Annotate the class to be a value type, and annotate the members that should or should not be included in the comparisons, etc. |
FYI https://github.com/google/built_value.dart is a new library that uses codegen to provide value types. |
Is there something similar to inline classes in Dart? |
No; this space is currently occupied by libraries that use codegen. |
I would probably stick with |
Without additional information we're not able to resolve this issue. Feel free to add more info or respond to any questions above and we can reopen the case. Thanks for your contribution! |
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.
The text was updated successfully, but these errors were encountered: