Add support for nullable static types in GDScript
**Describe the project you are working on:** This applies to many projects. This is an offshoot from https://github.com/godotengine/godot-proposals/issues/737, example use cases and other discussions are welcome. **Describe the problem or limitation you are having in your project:** Let's say you have a method that accepts a 2D position, which would look something like this: ```gdscript func whatever(vec): ``` A problem with this is that there's no type safety, so the function could unexpectedly break if the passed-in value is not a `Vector2`. One option is to use static typing: ```gdscript func whatever(vec: Vector2): ``` This works, and now it's not possible for users to, for example, pass in a `Color` or any other type that's invalid for this method. However, now you can't pass in `null` to mean N/A or similar. **Describe how this feature / enhancement will help you overcome this problem or limitation:** If GDScript's static typing system allowed specifying nullable types, we would be able to restrict the type to either a valid value or `null`. The presence of a valid value can then be detected simply by checking if it is not null, as non-null nullable typed values must be valid values. **Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:** My suggestion is to simply allow this by adding a question mark after the type name, which is the same syntax used in C#, Kotlin, and TypeScript. User code could look something like this
Posted by aaronfranke on Oct 16, 2019, 1:53 AM
Tags
Related issues
- Add 65536 size support for maps
- TypeScript 7.1 Iteration Plan
- nvim-treeconfig: upstream nvim-treesitter
- Add the "action" and "method" attributes to <button>
- [FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
- > We were imposed GitHub for so many exercises!
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
support (Software)
Add 65536 size support for maps
TypeScript 7.1 Iteration Plan
nvim-treeconfig: upstream nvim-treesitter
Add the "action" and "method" attributes to <button>
[FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
> We were imposed GitHub for so many exercises!
Add support for Visual Studio 2026 Integration
Support for Helm v4
Add support for nullable static types in GDScript
**Describe the project you are working on:** This applies to many projects. This is an offshoot from https://github.com/godotengine/godot-proposals/issues/737, example use cases and other discussions are welcome. **Describe the problem or limitation you are having in your project:** Let's say you have a method that accepts a 2D position, which would look something like this: ```gdscript func whatever(vec): ``` A problem with this is that there's no type safety, so the function could unexpectedly break if the passed-in value is not a `Vector2`. One option is to use static typing: ```gdscript func whatever(vec: Vector2): ``` This works, and now it's not possible for users to, for example, pass in a `Color` or any other type that's invalid for this method. However, now you can't pass in `null` to mean N/A or similar. **Describe how this feature / enhancement will help you overcome this problem or limitation:** If GDScript's static typing system allowed specifying nullable types, we would be able to restrict the type to either a valid value or `null`. The presence of a valid value can then be detected simply by checking if it is not null, as non-null nullable typed values must be valid values. **Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:** My suggestion is to simply allow this by adding a question mark after the type name, which is the same syntax used in C#, Kotlin, and TypeScript. User code could look something like this
Comments (0)
No comments yet. Start the discussion.