schultek / dart_mappable

Improved json serialization and data classes with full support for generics, inheritance, customization and more.
https://pub.dev/packages/dart_mappable
MIT License
163 stars 23 forks source link

Make copyWith Generic #221

Closed gabrielmcreynolds closed 2 months ago

gabrielmcreynolds commented 3 months ago

It would be very helpful if the generated copyWith function supported changing generics. For example, if I have a class that has a generic property, I would like to be able to change the generic type of the class with copyWith. Here is an example of what I have in mind:

oldInstance.copyWith<NewType>(
  // genericProperty was an instance of OldType
  genericProperty: newInstanceOfNewType
);

Currently, without the generics I get a type error saying:

TypeError: Instance of 'NewType': type 'NewType' is not a subtype of type 'OldType'

Thanks for your work on this package and not sure if this is possible but it would be helpful!

schultek commented 2 months ago

Closing this as duplicate of #54

I keep that one open as a feature request, but I probably won't get to it in a while. I would accept a PR though. The issue is this can't be a modification of the normal .copyWith, since a) there isn't something like "default generic params" which would break any existing code using it and b) the generic property would have to be required. So if impemented then as a new method e.g. ".copyWithType()".