GDScript: Implement soft access restriction for members prefixed with _ and __ (as warning by default)#98557
Conversation
8ec7d22 to
7a82944
Compare
d4f3cd9 to
eee809a
Compare
|
See also #98606 for another alternative |
HolonProduction
left a comment
There was a problem hiding this comment.
I don't think we should treat _ like private in other languages. It should behave like protected IMO.
There was a problem hiding this comment.
Please reset this change
37def5f to
7a4c746
Compare
|
Why |
|
Because it's the wrong name, you've added the same name twice, it added the one you missed |
_-prefixed members (as warning system by default)
_-prefixed members (as warning system by default)_-prefixed members (as warning by default)
Personally I prefer this one instead of #98606. Also, I think '__' should be private, not protected, because private access is very strict, and I think it will be rarely used - protected is enough in most cases |
In one word: it not only is coherent with the philosphy of godot's implementation of new features, but also greatly simplifies the code structure and maintains the code compatibility with the one in previous godot versions at the most extend. It's both technically and executabily simple, imo. |
|
Next I want to introduce |
14567f7 to
f79f00d
Compare
_-prefixed members (as warning by default)_-and-__-prefixed members (as warning by default)
f79f00d to
0ca39d5
Compare
_-and-__-prefixed members (as warning by default)_ and __ (as warning by default)
0ca39d5 to
ebde3e0
Compare
|
As some of updates contains with off-topic elements, I reverted them (except coloring the private and protected members exported in the inspector, which is kinda on-topic, but i think, yes, gray color should be more effective). Meanwhile, I fixed some incorrect codes that led to pollution to some other unit tests that should not trigger the warning. However, some of unit tests does calling |
12ad123 to
1e5cf3f
Compare
|
As the unit test of GDScript got an update (that was breaking-compatibility), the unit test should be reworked as I have time available for it... |
7403d60 to
d50b16d
Compare
|
Btw, I'm considering if it will be possible to get this pr suitable for 4.5, as currently 4.4 is going to enter the stage of beta cycle. Addition: |
I'm probably dumb, but that feature doesn't seem to be related to this PR's topic. Should it be implemented in a different PR instead? |
|
Bad news: |
d50b16d to
15d6657
Compare
|
After some discussion in the rocket chat, I'm planning to close this pr and supersedes this pr with a new one. The discussion result was that we should only leave |
Supersedes: #98136
Closes: godotengine/godot-proposals#641
After the discussion with some devs, with the philosophy of "implementing new features without game crashing". Here is the temporary discussion result in my opinion:
Rather than keywords and annotations, it's better to keep the original form that
_as prefix of private/protected members. There are three reasons for that:Reasons
_should be a protected method. At first, I thought this would be redundant, until the moment I imagined encapsulation on calling a custom virtual method:When you need to call a custom virtual method, it is better to call it through a wrapper.
2. From the perspective of technical reasons,
privateandprotectneed to do checks during the reduction. The original pr did checks by retracing the sourceGDScriptParser::Node, and even to retrace not only its super classes, but also the head attributes, which is much more performance-costly. However, by operating onp_identifier->name, we do not need to trace back what itsGDScriptParser::Nodeis and where it comes from; Instead, we just check if the class or the super classes has the member or the method. For protected identifiers, we just iterate the super classes ofparser->current_class, which greatly reduces the consumption of CPU performance. Compared with the implementation of original pr, this is of balanced cost.3. Based on the philosophy of "implementing new features without crashing the games", I turned the error into a warning. Of course you can turn it back to an error in GDScript warning settings. However, due to the fact that this pr is merely a soft implementation of access restriction, this does not break the runtime or throw an error if there is any invalid interclass access, so if your game runs out of your expectation, it's time to check your code, especially maybe somewhere you referenced a
_-prefixed variable?Implementation effect
When you trying to access a
_-prefixed (or__-prefixed) identifier outside the class that defines it, a warning (by default) will be thrown at the bottom of the script editor about trying accessing a private/protected member externally.Note: For attributes, this also take the invalid ones into consideration, e.g.:
For protected members, this warning would be eliminated if the accessor class is derived from the class where the target identifier is declared.
Discussions
I know that this would bring to thumbups from those who think this is correct, or thumbdowns from those who think that I'm standing for official devs. No matter what this post will be given with, feel free to share and discuss your ideas here.
Plans
Distinguishing color for(Off-topic)_-or-__-prefixed members in auto-completion.Distinguishing color for(Off-topic)_-or-__-prefixed exported members in the editor inspector.IntroducingI implement this before the For protected identifiers, we just iterate the super classes of__as the prefix of protected members (or private ones, needs to be discussed)parser->current_class. discussion on this. If this is no more needed, I'll remove this feature._and__.