-
Notifications
You must be signed in to change notification settings - Fork 211
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
Automatically generate copyWith and copyWithout when class contains only public final fields that are initialized with named arguments. #961
Comments
What if Dart add support of Data Classes and this should be part of it? |
@pedromassango I dont know exactly how a Data Class you talked about would behave. But if it just classes in the format that I described it could be classified automatically by the compiler without any special keyword. |
I don't think this belongs to the language specification. Maybe you could open an issue on the SDK side. A language-side solution is to implement a parameter spread operator, like JS one, where things like this would be possible: @copyable
class Person {
final String name;
final int age;
Person({this.name, this.age});
}
void main() {
final person0 = Person(name: 'Johan', age: 20);
final person1 = Person(...person0, name: 'Feldispato');
} This alternative has alredy been proposed (#893, maybe in other issues too). |
Sorry. I mean Data classes from Kotlin (https://kotlinlang.org/docs/reference/data-classes.html) |
Instead of "copyWithout" it would be better to use the following:
|
I'd really love for Dart to support the data classes and copy functionality outlined here in this post about C# 9. https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/ I've been building a lot of features in a flutter app recently, and I've found that the community and patterns have coalesced around immutable data types. As a result, we've found oursevles using freezed (https://github.com/rrousselGit/freezed) to provide data classes and "sum types" / "enums with associated data" since there's no story built into Dart for it yet. The user-land codegen required to support this functionality today makes me sad. It has a lot of quirks and it doesn't interact great with IDEs. It would be game-changing if Dart provided these features out of the box. |
Ya the new records from C# really make me jealous, it would be so amazing to just write: And have equality, copyWith, toJson and fromJson all in one. Doing it with meta-programming works, but is far more cludgy with fragile |
I love to have this. |
I'm going to close this out because I think it's mostly covered by other issues. We definitely know that many many users highly desire something like data classes in Dart, including some sort of record update (i.e. It's mostly a question of figuring out which approach is the right one and balancing trade-offs. I don't think it's likely that we would implicitly add |
The magic is wayyh better than having to manually implement a copyWith method for all entities. Anyway, I agree with you and since we're getting Data Classes and they will come with a copyWith we're all good 👍 |
Suppose we have a class called Person:
Automatically generate the following methods:
The copyWith method is like the ones we already know present in flutter plain objects and the copyWithout is the solution I came up with to the problem of not being able to set null value using copyWith (when the user wants to copy something to null he uses the argument with value equal to
true
. This feature could be a opt in with a decoration in the class, and would have compile time analysis if the class matches the rules described in the title: A class that contains only public final fields that are initialized with named arguments.The decoration could be something like:
And the compiler would be able to generate the copyWith and copyWithout methods and also make then available in the autocomplete for good developer experience.
Then with this feature I would be able to do this:
I believe this feature could help a lot in writing tests where we could have some complete filled object (with all fields set to a valid initial value) and wants to change just one field to test a condition. I really hope some sort of copying gets implemented. Because manually writing this methods feels wrong. Thanks in advance!
The text was updated successfully, but these errors were encountered: