-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
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
Remove REDUNDANT_FOR_VARIABLE_TYPE
warning
#81440
Remove REDUNDANT_FOR_VARIABLE_TYPE
warning
#81440
Conversation
04f9034
to
c5063ac
Compare
c5063ac
to
3ca6faa
Compare
I don't think it's right, this PR should contain 1 commit with My motivation for adding the warning was that specifying the
Specifying the type in each Perhaps it should be two warnings, one for the exclusive supertype, the second for the exact type. And both warnings should be enabled by default. |
You're right. I can remove the changes that modify aspects of
In my opinion, the intention of the loop variable type specifier is not clearly indicated, and as a result, might leave users confused about its proper use. Lots of languages use type indicators on their for loops (and I know that since GDScript is dynamically typed, and the type is inferred by the for loop analyzer regardless of annotation, it's a different story, but convention is important here in my opinion) Even statically typed languages which could reasonably infer the type of an iterator typically require an annotation anyways: // C# code snippet
List<string> greetings = new List<string>(){"a", "b", "c", "d"};
foreach(string greet in greetings) {
// code here
} For users coming from other languages, they might become confused as to why explicitly naming the type of their iterator is not recommended. At the very least it should probably be documented somewhere, perhaps in one of our sections about static typing. If it's already documented there let me know.
I think it increases readability, because not every list has a type that is immediately obvious to a reader given the name of the list. It gives the user an immediate heads up on what things it can do with the iterator variable, given the type annotation. As for helping users realize they don't need an annotation for a hard static type, I don't know the best way to get that information across, but I don't think we should be discouraging attempts at ensuring a hard static type (even if they're potentially misguided)
I could be convinced that having two warnings would be a good idea. I'll have to think on it more! |
3ca6faa
to
549e8ac
Compare
I have thought about whether I think having two warnings is necessary, and I still don't see the point in a redundant for variable warning. However, if one were to exist, I don't think we should set it to |
REDUNDANT_FOR_VARIABLE_TYPE
warning to FOR_VARIABLE_EXPLICIT_SUPERTYPE
REDUNDANT_FOR_VARIABLE_TYPE
warning to FOR_VARIABLE_EXPLICIT_SUPERTYPE
REDUNDANT_FOR_VARIABLE_TYPE
warning, to add instead FOR_VARIABLE_EXPLICIT_SUPERTYPE
Well, you can only do 4 things with inferred type:
I agree that point 2 may be a valid need if the user prefers a more verbose style and that it is reasonable to distinguish between points 2 and 3. But I'm still not sure if this is a style we should encourage. In my opinion, inferring the Whether or not a warning is necessary in case 2, I suggest discussing it at the GDScript team meeting (and in this thread before). I don't have a clear opinion on this issue. |
The current style guide already encourages adding a type when it might not be "clear" what it is:
...
# What type is this? It's not immediately clear to the reader, so it's bad.
var value := complex_function() As there's no warning for not using
|
I'm going to chime in on this and say that I do find the original warning to be quite useless, and removing it in favour of this PR's warning makes complete sense. If REDUNDANT_FOR_VARIABLE_TYPE is on by default when I get my hands onto 4.2, I would turn it off in a heartbeat for each new project of mine, or alternatively be so annoyed by it that I'm going to forget about the feature entirely (bit of exaggeration, of course). This is not from an "internal workings of Godot" perspective, mind you, Rather, it's from the average user's perspective, that just would like to write slightly more verbose code on the following few occasions:
|
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.
I agree with this PR and will approve it after some fixes. The combined warning was my mistake. The warning for strict supertype definitely makes sense. The warning for equal types is controversial, so we shouldn't add it without consensus.
Also should add tests for this case (modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.gd
).
Thanks for the PR @ryanabx and thanks for the comments to all participants!
I know that |
My thoughts after the meeting (about removing the warning entirely, and not just for equal types). The for i in range(5):
i = "p_" + str(i) # Error, you cannot assign a string because `i` is **hard** int. Now you can remove an inferred type by specifying for i: Variant in range(5):
i = "p_" + str(i) # OK. You can simply declare a new variable instead of reusing the same variable. Languages generally recommend declaring new variables rather than reusing old ones. for i in range(5):
var s: String = "p_" + str(i) # OK. I still think the warning improves code style specifically for the @ryanabx @adamscott @vonagam But I don't mind removing the warning entirely if a lot of people support it. We may add a warning later when we reach consensus. |
I am for removing the warning. |
The way I see it, there are two use cases for specifying a super type instead of the inferred subtype:
I'm sure about my position on removing the warning for when the annotation is the same as inferred, but I'm still gathering opinions on the super type warning. |
After testing i came here looking for this, or a reason why there was a warning. I agree with others that there shouldn't be a warning. Specifically because i like to ensure everything is typed and explicit anyway, which to me makes the warning obsolete. I think this should also be encouraged. As i went back through my code to add these i noticed places where i wasn't sure of the type, but once added i had the warning saying it was already inferred. If i the programmer did not know it was inferred and did not know what type it was at a glance, then i need to ensure i add the type. If i can turn it off then its not a massive problem but i was still confused by it, so i am sure plenty of others will be too. |
549e8ac
to
902bc3e
Compare
REDUNDANT_FOR_VARIABLE_TYPE
warning, to add instead FOR_VARIABLE_EXPLICIT_SUPERTYPE
REDUNDANT_FOR_VARIABLE_TYPE
warning, to add instead FOR_LOOP_VARIABLE_SUPERTYPE_SPECIFIED
735ae5b
to
a8dcdb8
Compare
I have updated the commit, rebasing to match current master, and making a few modifications: The warning for strict supertype is now I also opted to make the warning EDIT: For those of you just coming into the discussion, most of the discussion has moved to whether or not an explicit supertype warning is needed: i.e. for i: Variant in range(4): # Warning? Or not? Most, if not all of us have agreed now that a redundant type warning is not needed, and should be removed: i.e. for i: int in range(4): # No more warning |
a8dcdb8
to
04588a6
Compare
Making it In problematic examples of such sort: for i in range(5):
i = ... I see "the code smell" in the assignment to iteration variable here and typing is irrelevant. Yes, with And in general I do think that it is more productive to talk about |
You're right. I think my tendency is to want to compromise even if one person is a holdout, which is not always the best thing to do. I haven't seen anyone else state that they are for such a warning, nor do I personally think a warning for any explicit typing of for loop variables is necessary. Pending review from @adamscott , @dalexeev , or other members of the team, I will just remove all warnings of the sort for now. If there are any more opinions to be shared, this discussion can remain open :)
Yes, I don't think changing i at all is good practice. For other reasons, static typing with a supertype might be a good idea (the reasons I mentioned above)
True, for my personal opinion which aligns with yours (no warning should be necessary for either option) I felt like any example explains my point well enough, but I understand that a better example would help convey the point much easier. |
Remove REDUNDANT_FOR_VARIABLE_TYPE
04588a6
to
ceda960
Compare
REDUNDANT_FOR_VARIABLE_TYPE
warning, to add instead FOR_LOOP_VARIABLE_SUPERTYPE_SPECIFIED
REDUNDANT_FOR_VARIABLE_TYPE
warning, ~~to add instead FOR_LOOP_VARIABLE_SUPERTYPE_SPECIFIED
~~
REDUNDANT_FOR_VARIABLE_TYPE
warning, ~~to add instead FOR_LOOP_VARIABLE_SUPERTYPE_SPECIFIED
~~REDUNDANT_FOR_VARIABLE_TYPE
warning
Thanks! |
Remove
REDUNDANT_FOR_VARIABLE_TYPE
Change REDUNDANT_FOR_VARIABLE_TYPE to FOR_LOOP_VARIABLE_SUPERTYPE_SPECIFIEDThese changes are still open to discussion!
Currently,
REDUNDANT_FOR_VARIABLE_TYPE
warns the user when they explicitly type a for loop variable as the same type as what's inferred by the analyzer. This discourages the user from explicitly typing their for loop variables:It is a common convention in statically typed languages to annotate for loop variables, even if the iterator variable can be inferred by the analyzer:
https://www.w3schools.com/cs/cs_foreach_loop.php
https://www.geeksforgeeks.org/g-fact-40-foreach-in-c-and-java/#
Since it is a convention in modern statically typed languages, statically typed GDScript would be the exception to the general concensus. I have no problem with letting the analyzer infer the type of the iterator variable, but I do not think that a warning should be issued when a type annotation is used for a loop and the explicit and inferred types match.
EDIT 9/12/23: The discussion having progressed, seems to be leaning towards removing the
REDUNDANT_FOR_VARIABLE_TYPE
warning in its entirely. For those people behind in the discussion, it was previously discussed whether or not we add a warning for the strict supertype of an inferred variable instead, for example:Most people seem to be of the view that no warning should be necessary in any case of explicitly typing a for loop variable.
As always, more discussion is welcomed.
Please let me know your thoughts! 😄