schultek/dart_mappable

Make copyWith Generic

gabrielmcreynolds opened this issue · 1 comments

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!

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()".