-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Flag unintended field over field overrides #28810
Comments
Where do folks stand on this? cc @munificent @floitschG @lrhn @eernstg |
It's not an error, but I'm agnostic to lints since I never use them. |
There's certainly an overlap, but those issues are about allowing field overrides, e.g., setter-overrides-field (say, to perform some validation on the values assigned to that field), or getter-overrides-field (say, to log usages), etc. Even though field-overrides-field is a case which is difficult to justify (it's very likely to be a mistake which just creates some confusion and wastes some space), it is not singled out in the work done for those issues, and in particular it's not an error nor a warning. I think we should keep a rather tight budget on making any notion of "stupid code" an error, it's much better to take a softer approach like a lint on that type of situation. The problem is that (1) there is no end to the number of situations we could cover if we start squeezing stupid code out of the language per se, and (2) some times we will get it wrong and outlaw some really useful corner cases. As far as I can see, the overridden_fields lint, does actually check for field-overrides-field, but its documentation is sufficiently vague to allow several other interpretations: the description is a plain 'Do not override fields', and the reader can't really generalize from the examples. So I think the existing lint is a good solution, but it needs to have better documentation. |
Sorry, I wasn't clear enough. Lints have nothing to do with the language team, other than as evidence of demand for features that we have chosen not to provide (e.g. The question is whether or not we want to make field/field overrides a language level error (as they are in swift, for example). The argument for doing so is that it's a performance and correctness trap, and is essentially never what the user intended. It sounds like @eernstg and @lrhn are against adding this as a language error. Anyone else have thoughts? |
Indeed. |
I agree with @lhrn, and for the stated reasons. That being said, I think we have clearly set a usability trap for our users since we see that they do seem to override fields with fields even though it virtually never does what they want. I believe that's because the syntax for declaring an "abstract field" is painfully long and redundant. A better syntax for that, or properties in general, might help a lot. |
Better syntax would be great. int get set x; to extend your abstract getter Or we could move to a C#-like syntax like int _num;
String str {
get => "$_num";
set => numx = int.parse(_); // Using implicit argument for the => function!
} |
I didn't get enough time to polish it off, but I started working on a proposal in this direction. It gets complex when you consider all of the different flavors of "property" a user might want to define, but I think there's something promising there. Not having to repeat the name and type between the getter and setter would be fantastic. |
We could of course add something like However, we can also go pretty far using the current syntactic style. Lasse already mentioned the abbreviation |
Ok, so it seems like there is no consensus for moving forward on this. Closing as not planned. |
There are concerns about the usability of field over field overrides. It seems too easy to accidentally hide fields and in most cases field over field overrides are unintended.
Three options have been discussed:
The text was updated successfully, but these errors were encountered: