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

GDScript: Add C#-style property setter/getter shorthand #99199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dalexeev
Copy link
Member

@dalexeev dalexeev commented Nov 13, 2024

The following:

var position = Vector2()
var x => position.x
var y => position.y
var length => position.length()

is a shorthand for:

var position = Vector2()

var x:
    set(value):
        position.x = value
    get:
        return position.x

var y:
    set(value):
        position.y = value
    get:
        return position.y

var length:
    set(_value):
        assert(false, 'Cannot assign a value to property "length".')
    get:
        return position.length()

A valid setter is generated for "lvalue" expressions (identifiers and subscripts), for non-"lvalue" expressions the setter has an assert. It is also planned to add an analyzer error when assigning a "readonly" property (not yet implemented).

Type specifiers, type inference, and static variables are supported:

var a: int => _a
var b :=> _b
static var c => _c

This might be useful together with the new @export_tool_button annotation, as there are issues with self referenced lambdas stored in properties of RefCounted-based objects. See godotengine/godot-proposals#11147 and #97834.

Q&A

  • Are local variables supported?
    • No, like other setter/getter styles, it only supports member variables. This would require a completely different implementation for local variables and is actually quite controversial if we want to add control flow optimizations in the future.
  • Is it possible to have granular control for setter and getter? For example, public getter and private setter?
    • No, GDScript does not support private variables, and you can't do this with other setter/getter styles.
  • Is it possible to customize the setter behavior (e.g. emit a signal)?
  • Is it possible to exclude the setter generation even if the expression is an "lvalue"?
  • Are setters/getters with the shorthand notation inlined?
    • No, although this form has special qualities that are convenient for potential inlining, it is not currently implemented. Also, removing a frame from the call stack can be inconvenient in debug versions.

TODO

  • Open a proposal.
  • Check non-"lvalue" identifiers/subscripts (like constants).
  • Add static analysis when assigning a "readonly" property.
  • Add annotation validation (e.g. @onready doesn't make sense).
  • Add tests.

@dalexeev dalexeev added this to the 4.x milestone Nov 13, 2024
@dalexeev dalexeev requested a review from a team as a code owner November 13, 2024 20:59
@dalexeev dalexeev force-pushed the gds-add-setget-shorthand branch from b7f36b8 to 8d1ba5d Compare November 13, 2024 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant