Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: explain the usage of copyWith as a clone method #254

Open
rsevero opened this issue Jan 15, 2025 · 0 comments
Open

Documentation: explain the usage of copyWith as a clone method #254

rsevero opened this issue Jan 15, 2025 · 0 comments
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@rsevero
Copy link

rsevero commented Jan 15, 2025

dart_mappable copyWith generated methods can be used as clone tools but because of the overidding of the == operator, such a clone is recognized as equal to the original object when compared with the == operator. Only the identical() method detects the clone and the original as diferent objects.

I believe it would be helpful to users to include a section on the copyWith dart_mappable doc page explaing that. I suggest something like:


Clone

copyWith can be used as a clone tool when called without any parameters but be aware that the overriden == operator will detect the clone and the original as the same object. Only the identical() method detects that they are diferent objects.

Example:

@MappableClass()
class Foo with FooMappable {
  final int value;

  Foo(this.value);
}

void main() {
  var foo1 = Foo(1);
  var foo2 = foo1.copyWith();

  assert(foo1 == foo2); // succeeds
  assert(identical(foo1, foo2)); // fails
}
@schultek schultek added documentation Improvements or additions to documentation good first issue Good for newcomers labels Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants