-
-
Notifications
You must be signed in to change notification settings - Fork 21.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
Add support for GDScript Option type/syntax #23719
Comments
Related to #7223 |
bojidar-bg, so far I understand null-coalescing operator it's only a shortcut for if x!=Null then x else something. But here it's not x, it's a chain of multiple method calls which some of them could return null and then throw a null pointer exception (invalid index attribute in 'Nil' in Godot terms). The null-coalescing operator wouldn't help with that. |
I just linked the other issue, since they are both about null-handling and operators. They are not about the same thing, just about similar things. |
In case we go the second way, I would also like to suggest that |
Having a nullable shorthand is really necessary for readibility when you implement null into the language. If Godot plans to implement something akin to Dart's type soundness, it be beneficial to as well to have an Optional or Nullable token. |
Related to #27261 as well, consider discussing nullable types there. |
Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine. The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker. If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance! |
Proposal already exists as godotengine/godot-proposals#162. |
I would like to suggest a feature that exists in a few modern programming languages and which I think could be very useful. It's a safe navigation operator paradigm where you manipulate an object without having to worry if it's null and that would throws an unwanted null pointer exception. The typical use case is a chain getter calls, like objectA.getObjectB().getObjectC().get... and the wanted result is either the value when all those objects exist or a default value.
Instead of implementing a check for each getter to ensure that the object returned by the previous getter is not null, which would look like:
And even more verbose when there are arrays, instead, it would be much safer, shorter and more convenient to write something like this:
Instead of throwing an exception for a very special case you didn't foresee in your code, the nullable object could simply return a default value when not found. That would allow to reduce greatly the amount of checks that a variable or result of a method is not null.
I know that in Java it's the class Optional that does such feature, using map to call safely the function. I heard that C# implemented it long ago in a nice way. And groovy and kotlin integrated it also with ?. Either way (with ? ?: operator or a wrapper class like Optional with map()) are fine for me.
The text was updated successfully, but these errors were encountered: