-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add method hints for non-nullable return types and nullable arguments #2241
Comments
Couple of observations:
So it can become quite cumbersome in scenarios where we would always know that no nil can ever be produced. To achieve this, we would need to do an audit of every API entry point, to annotate properly which arguments can be NULL, and which methods could return NULL. In the argument case, the good news is that there is extensive use of macros that indicate that this is an error condition (like |
I am all for that addition. |
Godot sadly does not annotate their functions and properties to include information whether they are optional or not. This is an issue that has been raised a long time ago and has gained little to no traction: godotengine/godot-proposals#2241 This poses a problem, because certain Godot APIs would return nil, and I would try to create an object out of it, and crash, while a nil return is ok. It also posed a problem for APIs that could take a nil parameter, but instead, in SwiftGodot, you had to create an instance of the object, even if it is unnecesary or not the desired outcome. So I have now settled on an evolutionary plan: * For now, all reference types that could have been nil either as a parameter or a return value, are declared as swift optionals. * I have introduced a hardcoded list of method return types and method arguments that can be used to flag exceptions to this: scenarios where we can verify that the return value would never be nil, or the parameter demands a non-nil argument. The list currently is hardcoded in Swift, but will eventually move to a Json file to make it easier to work with. Sadly, this means that there will be some churn, until all this is properly validated. This is a fix for this issue: #63 Some additional data: * Some 114 property returns were affected * Some 432 method returns were affected
Godot sadly does not annotate their functions and properties to include information whether they are optional or not. This is an issue that has been raised a long time ago and has gained little to no traction: godotengine/godot-proposals#2241 This poses a problem, because certain Godot APIs would return nil, and I would try to create an object out of it, and crash, while a nil return is ok. It also posed a problem for APIs that could take a nil parameter, but instead, in SwiftGodot, you had to create an instance of the object, even if it is unnecesary or not the desired outcome. So I have now settled on an evolutionary plan: * For now, all reference types that could have been nil either as a parameter or a return value, are declared as swift optionals. * I have introduced a hardcoded list of method return types and method arguments that can be used to flag exceptions to this: scenarios where we can verify that the return value would never be nil, or the parameter demands a non-nil argument. The list currently is hardcoded in Swift, but will eventually move to a Json file to make it easier to work with. Sadly, this means that there will be some churn, until all this is properly validated. This is a fix for this issue: #63 Some additional data: * Some 114 property returns were affected * Some 432 method returns were affected
Copying some discussion from the #gdextension channel on RocketChat: @akien-mga wrote:
And I wrote:
This would be good to discuss at a (or several) GDExtension meetings! I've added it to the agenda. |
We discussed this at the GDExtension meeting earlier (sorry, I forgot to comment during the meeting) and came up with the following next steps:
|
From everything I've read so far, there seems to be two primary routes. One is utilizing both |
See also #162 and #2242.
Describe the project you are working on
The Godot editor 🙂
Describe the problem or limitation you are having in your project
Currently, GDNative bindings must assume that all non-primitive return types (such as Node) may be
null
. This is problematic for safety-oriented languages such as Rust and Swift as it leads to uglier and unsafe code.Describe the feature / enhancement and how it helps to overcome the problem or limitation
null
. This hint should only be added to methods that return Object-derived types, not primitive types such asint
orVector2
as these are non-nullable by design. If this hint is not present, the method is assumed to have a nullable return type (for Object-derived types only).null
. If this hint is not present, the argument is assumed to be non-nullable.This information can also be displayed in the generated class reference to improve documentation.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
METHOD_FLAG_RETURN_NON_NULL
.GodotTypeInfo::Metadata
).If this enhancement will not be used often, can it be worked around with a few lines of script?
No, as this is core engine functionality.
Is there a reason why this should be core and not an add-on in the asset library?
This functionality is part of the engine itself.
The text was updated successfully, but these errors were encountered: