-
-
Notifications
You must be signed in to change notification settings - Fork 21k
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
C#: Enable nullability for generated & compatibility code #88340
base: master
Are you sure you want to change the base?
C#: Enable nullability for generated & compatibility code #88340
Conversation
Finally came to look at this, I think it looks good. Would have to be rebased to be properly tested. |
Thanks for the PR, and thanks @paulloz for taking a look. A couple comments:
Adding However, I'm more worried about adding That said, as I understand it, nullability in parameters is not about what's allowed but about intention. For example, if you have a method I feel like all these nullability annotations are only being added because we know the GodotObject can be null, but we don't know if it should. I think this is probably impossible to automatically determine if we don't have ClassDB telling us this information. Here's some guidance for adding nullability annotations: https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/api-guidelines/nullability.md#annotation-guidance
I don't think this is true, I can imagine it can be null if it the requested singleton wasn't registered (like editor singletons in a game running outside the editor). Do we throw if the retrieved singleton is null? In that case it's fine to consider them always being non-null.
I don't know about this, I would have to check. |
Arguably, this is implicitly how it currently works: we return reference types from a |
• Specify explicit references to GodotObject as nullable in Variant
c904e30
to
4278d3a
Compare
For the most part, this is true, and for the most part this is also how the PR handles things. The sole exception is
I agree, which is why this PR isn't even attempting to do so.
Wouldn't editor singletons be part of
I guess more specifically, I suppose, with that in mind, this PR could be adjusted to make anything |
That's why I said it's fine for return types, but for parameters I think the safest option is to never allow null just in case the method doesn't accept it. Either way I feel like being oblivious may be better since it would communicate to the user that we don't know if it can be null or not, and I think that's better than saying we know but we're actually just guessing.
Yes but that's only a difference we make in C#. What I was trying to say is there could be singletons in the engine that are never assigned under certain circumstances and therefore remain null. Whether this is something that happens in practice or not I don't know, but it seems like it's at least possible.
Not having check simply means we were probably happily returning null before, since the API wasn't annotated everything can be implicitly null. In this case I think the intention was for singletons to not be null, so I think you made the right assumption. But maybe we should check and throw an exception otherwise, although this would break compat (but I'd expect not many users rely on this behavior).
I think
I haven't looked in depth at that PR but it looks like it adds the nullability information to ClassDB so I think that'd be perfect. It may be better to way until then to add nullability annotations to generated code, I'm a bit wary of adding nullability annotations that we'd have to change later since that could be considered breaking compat (although a bit minor probably). |
Extracted from #83117 to focus exclusively on allowing generated/compatability code be null-aware. This trims out a lot of extra fluff from that PR, as it doesn't put any real focus on any internal system; this is strictly for the exposed layer of the API. As such, it focuses on these key points that are always true in the context of Godot:
Object
class.null
.null
outside of its ownObject
class.Yes, Godot should ideally have a means of specifying if a given
Object
is guaranteed to be non-nullable, but that's not what this PR is about. What it is about is unambiguously representing the inherent nullability of Godot's classes.