rrousselGit / freezed

Code generation for immutable classes that has a simple syntax/API without compromising on the features.
https://pub.dev/packages/freezed
1.94k stars 237 forks source link

Operator overloading #1135

Closed jimmyff closed 1 week ago

jimmyff commented 1 week ago

Having difficulties figuring out how to add artihmetic operators to a freezed class. See below:


@freezed
class ImageOffset with _$ImageSize {
  const ImageOffset._();
  const factory ImageOffset({
    required double dx,
    required double dy,
  }) = _ImageOffset;

  factory ImageOffset.fromJson(Map<String, Object?> json) =>
      _$ImageOffsetFromJson(json);

  static ImageOffset get zero => const ImageOffset(dx: 0, dy: 0);

  // Problematic code
  @override
  ImageOffset operator +(covariant ImageOffset other) =>
      ImageOffset(dx: this.dx + other.dx, dy: this.dy + other.dy);
}

Gives the following errors:

[{

    "owner": "_generated_diagnostic_collection_name_#5",
    "code": {
        "value": "undefined_getter",
        "target": {
            "$mid": 1,
            "path": "/diagnostics/undefined_getter",
            "scheme": "https",
            "authority": "dart.dev"
        }
    },
    "severity": 8,
    "message": "The getter 'dx' isn't defined for the type 'ImageOffset'.\nTry importing the library that defines 'dx', correcting the name to the name of an existing getter, or defining a getter or field named 'dx'.",
    "source": "dart",
    "startLineNumber": 170,
    "startColumn": 28,
    "endLineNumber": 170,
    "endColumn": 30
},{
    "owner": "_generated_diagnostic_collection_name_#5",
    "code": {
        "value": "undefined_getter",
        "target": {
            "$mid": 1,
            "path": "/diagnostics/undefined_getter",
            "scheme": "https",
            "authority": "dart.dev"
        }
    },
    "severity": 8,
    "message": "The getter 'dx' isn't defined for the type 'ImageOffset'.\nTry importing the library that defines 'dx', correcting the name to the name of an existing getter, or defining a getter or field named 'dx'.",
    "source": "dart",
    "startLineNumber": 170,
    "startColumn": 39,
    "endLineNumber": 170,
    "endColumn": 41
},{
    "owner": "_generated_diagnostic_collection_name_#5",
    "code": {
        "value": "undefined_getter",
        "target": {
            "$mid": 1,
            "path": "/diagnostics/undefined_getter",
            "scheme": "https",
            "authority": "dart.dev"
        }
    },
    "severity": 8,
    "message": "The getter 'dy' isn't defined for the type 'ImageOffset'.\nTry importing the library that defines 'dy', correcting the name to the name of an existing getter, or defining a getter or field named 'dy'.",
    "source": "dart",
    "startLineNumber": 170,
    "startColumn": 52,
    "endLineNumber": 170,
    "endColumn": 54
},{
    "owner": "_generated_diagnostic_collection_name_#5",
    "code": {
        "value": "undefined_getter",
        "target": {
            "$mid": 1,
            "path": "/diagnostics/undefined_getter",
            "scheme": "https",
            "authority": "dart.dev"
        }
    },
    "severity": 8,
    "message": "The getter 'dy' isn't defined for the type 'ImageOffset'.\nTry importing the library that defines 'dy', correcting the name to the name of an existing getter, or defining a getter or field named 'dy'.",
    "source": "dart",
    "startLineNumber": 170,
    "startColumn": 63,
    "endLineNumber": 170,
    "endColumn": 65
}]

Thanks

jimmyff commented 1 week ago

My bad, typo in with - wrong class part!