dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

Enabling deep copyWith on custom collection/iterables

Open point-source opened this issue 1 year ago • 4 comments

Is there a way to enable dart_mappable to support deep copy on custom iterables and collections (such as those from fast_immutable_collections)? There is currently a way to support serialization on these custom types via custom mappers and it would be great to extend this type of support to the deep copy feature as well.

Specifically to enable syntax such as:

final myinstance = MyClass(myCustomIterable: <String>[]);
final MyClass myInstanceCopy = myinstance.copyWith.myCustomIterable.add('string');

point-source avatar Oct 20 '22 20:10 point-source

I can look into it. I think the problem with fic is that they don't implement the normal list and map mixins, so any implementation needs to be specific to fic.

schultek avatar Oct 21 '22 09:10 schultek

Yeah that's why I was thinking about having a class similar to the way BaseMapper works. Then the user could implement each method necessary.

point-source avatar Oct 22 '22 02:10 point-source

I've added an example on the feat/support-fic branch. It uses a new SerializableMapper that works with json_serializable-style classes. It also sets up a custom copyWith extension.

Right now there is no new code-generation, but all manual setup. But i can see how the wiring for the copywith extension could be generated for the ImmutableListCopyWith class with some extra annotation.

schultek avatar Oct 22 '22 18:10 schultek

Wouldn't this work for FIC?

final MyClass myInstanceCopy = myinstance.copyWith.myCustomIterable.apply((iterable) => iterable.add('string'));

AlexanderFarkas avatar Jun 08 '23 09:06 AlexanderFarkas