Skip to content
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

Closed
gokudomatic opened this issue Nov 15, 2018 · 9 comments
Closed

Add support for GDScript Option type/syntax #23719

gokudomatic opened this issue Nov 15, 2018 · 9 comments

Comments

@gokudomatic
Copy link

gokudomatic commented Nov 15, 2018

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:

if objectA!=Null and objectA.getObjectB()!=Null and objectA.getObjectB().getObjectC()!=null and ...:
    return objectA.getObjectB().getObjectC().get...
else:
    return defaultValue

And even more verbose when there are arrays, instead, it would be much safer, shorter and more convenient to write something like this:

return objectA?.getObjectB()?.getObjectC()?.get... ?: defaultValue

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.

@bojidar-bg
Copy link
Contributor

Related to #7223

@gokudomatic
Copy link
Author

gokudomatic commented Nov 15, 2018

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.

@bojidar-bg
Copy link
Contributor

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.

@gokudomatic
Copy link
Author

In case we go the second way, I would also like to suggest that some_variable? always return an Optional variable which wraps the real variable. Calling an attribute of an optional would always be returned as an Optional and therefore it should have the ? for clarity. This Optional would have methods like get and is_present. And the ?: would return either the get of the last optional if is_present is true or else the given default value. If no default value is given behind the ?:, I suggest to return Null.
About arrays, I don't know what's the best.

@GammaGames
Copy link

GammaGames commented Apr 30, 2019

I think this would be a useful feature, here's some more examples in other languages:

@Spartan322
Copy link
Contributor

Spartan322 commented Apr 30, 2019

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.

@bojidar-bg
Copy link
Contributor

Related to #27261 as well, consider discussing nullable types there.

@clayjohn
Copy link
Member

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!

@bojidar-bg
Copy link
Contributor

Proposal already exists as godotengine/godot-proposals#162.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants