Skip to content

Simple syntax for defining value objects #10551

Closed as not planned
Closed as not planned
@DartBot

Description

@DartBot

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).needs-infoWe need additional information from the issue author (auto-closed after 14 days if no response)type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions