Skip to content
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

Closed
DartBot opened this issue May 9, 2013 · 9 comments
Closed

Simple syntax for defining value objects #10551

DartBot opened this issue May 9, 2013 · 9 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). needs-info We need additional information from the issue author (auto-closed after 14 days if no response) type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented May 9, 2013

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.

@dgrove
Copy link
Contributor

dgrove commented May 9, 2013

Set owner to @gbracha.
Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented May 9, 2013

I think this is a dup of issue #501, though it is not all clear from the description. Please have a look and see if value type support is what you are really after. This of course is about much more than syntax or ==/hashCode.


Added NeedsInfo label.

@DartBot
Copy link
Author

DartBot commented May 10, 2013

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.

@DartBot
Copy link
Author

DartBot commented May 15, 2013

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.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). needs-info We need additional information from the issue author (auto-closed after 14 days if no response) labels May 15, 2013
@davidmorgan
Copy link
Contributor

FYI https://github.com/google/built_value.dart is a new library that uses codegen to provide value types.

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed Priority-Medium labels Mar 1, 2016
@munificent munificent changed the title Provide simplified syntax for defining and implementing value objects Simple syntax for defining and implementing value objects Dec 16, 2016
@munificent munificent changed the title Simple syntax for defining and implementing value objects Simple syntax for defining value objects Dec 16, 2016
@SAGARSURI
Copy link

Is there something similar to inline classes in Dart?

@davidmorgan
Copy link
Contributor

No; this space is currently occupied by libraries that use codegen. built_value, which was new when I commented nearly five years ago, is no longer new but quite popular ;) it has a relatively new competitor called freezed.

@SAGARSURI
Copy link

I would probably stick with build_value instead of using a new library. I am sure build_value is more tested in the wild than freezed. But yeah thanks for the info.

@lrhn lrhn removed the P2 A bug or feature request we're likely to work on label Aug 31, 2020
@github-actions
Copy link

github-actions bot commented Sep 1, 2023

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!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). needs-info We need additional information from the issue author (auto-closed after 14 days if no response) type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants