numen31337/copy_with_extension

CopyWith on records

Maksimka101 opened this issue ยท 4 comments

Records are coming in the dart 3. They are pretty useful and the almost only drawback is the inability to copy them. I believe it's possible to implement as records can have extensions

So I imagine this api:

@copyWith
typedef User = ({
  String id,
  String name,
});

void main() {
  final User user1 = (id: 'id', name: 'name');
  final User user2 = user1.copyWith(id: 'id2');
}

If I'm not mistaken, according to the feature specification a Record can't be extended as per:

The type Record refers to a built-in class defined in dart:core. It has no instance members except those inherited from Object and exposes no constructors. It can't be constructed, extended, mixed in, or implemented by user-defined classes.

It might be a useful feature, but apparently we can't implement it here. Please let me know if I'm missing something.

As you can see, It is possible to create an extension on a record:

image

Sorry for screenshot

Btw, I have an experience with code generation. So can't guarantee anything, but probably I can try to implement it if you are too busy

I'm going to have a look at it. Not sure about the timing tho.