-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
feat: copyWith
methods for all classes
#853
base: master
Are you sure you want to change the base?
Conversation
copyWith
methods for all classescopyWith
methods for all classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @g123k!
- I don't believe in "just in case" code, that nobody asked for and that
we'llI'll have to maintain, cf. here 38 files out of 39 - your implementation of
copyWith
is misleading as you're not really copying data - that could be OK forint
andString
but not forMap<String, String>
that is passed by reference
Example:
class Toto{
Map<String, String>? map;
Toto({this.map});
Toto copyWith({
Map<String, String>? map,
}) {
return Toto(
map: map ?? this.map,
);
}
}
void main() {
final Toto toto = Toto();
toto.map = {'a': 'toto'};
print(toto.map);
final Toto titi = toto.copyWith();
titi.map!['a'] = 'titi';
print(toto.map);
}
Output:
{a: toto}
{a: titi}
That's partly why I used a combo of jsonDecode
/ jsonEncode
to recreate objects from scratch. The second reason is that it's much easier to read and maintain. And performance is not an issue here.
True, you're right about the collections which are mutable. |
So you agree that your code contains bugs, right?
That's what I call "just in case" code, and I don't believe in it. Let's rather code "on demand": issues are made for that.
We agree that we disagree. If you need an additional reason NOT to do that: in the specific case of |
Hi everyone!
Instead of limiting ourselves to the
Product
class, I've added thecopyWith
method to all classes, except when: