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 shorthand syntax for dictionary keys in GDScript #11638

Open
Emaxoso opened this issue Jan 25, 2025 · 2 comments
Open

Add shorthand syntax for dictionary keys in GDScript #11638

Emaxoso opened this issue Jan 25, 2025 · 2 comments

Comments

@Emaxoso
Copy link

Emaxoso commented Jan 25, 2025

Describe the project you are working on

I am developing a game using Godot 4, where data structures such as dictionaries are extensively used to manage information about characters, items, and various in-game elements.

Describe the problem or limitation you are having in your project

Currently, when creating dictionaries in GDScript, I must explicitly write each key-value pair, even if the keys match the variable names. This results in verbose and repetitive code, especially when dealing with objects that have multiple properties. For example:

var name = "Mario"
var age = 30
var character = { "name": name, "age": age }

This verbosity makes the code less readable and harder to maintain, especially when there are many properties involved.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I propose adding a Property Shorthand Syntax to GDScript, similar to what exists in JavaScript (ES6). This syntax would allow developers to create dictionaries or objects more concisely when the keys are identical to the variable names.

For example, the code above could be simplified as follows:

var name = "Mario"
var age = 30
var character = { name, age }

This enhancement would:

  1. Improve code readability and reduce verbosity.
  2. Speed up development by removing redundant code.
  3. Align GDScript with modern scripting language conventions, making it more intuitive for developers familiar with other languages like JavaScript or Python.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

The implementation would involve extending the parser for dictionary creation in GDScript to recognize when a variable name is used without explicitly defining a key.

Current Syntax:

var name = "Mario"
var age = 30
var character = { "name": name, "age": age }

Proposed Syntax:

var name = "Mario"
var age = 30
var character = { name, age }

Pseudo-Code for Parser Adjustment:

When parsing dictionary entries:

  1. If the entry is written as { variable_name }, automatically expand it to { "variable_name": variable_name }.
  2. Ensure compatibility with existing dictionary syntax and avoid conflicts with other GDScript features.

Examples:

  • Single variable shorthand:

    var score = 100
    var data = { score }

    Expands to:

    var data = { "score": score }
  • Mixed shorthand and explicit keys:

    var name = "Mario"
    var age = 30
    var character = { name, age, "role": "hero" }

If this enhancement will not be used often, can it be worked around with a few lines of script?

While the current syntax is functional, it is verbose and requires repetitive code. There is no simple workaround to achieve the same level of readability and conciseness.

Is there a reason why this should be core and not an add-on in the asset library?

This feature is a language-level enhancement, not something that can be implemented as an add-on. It requires modifications to the GDScript parser and is fundamental to how dictionaries are written in the language. Adding it to the core will benefit all GDScript users by improving the overall developer experience.

@Meorge
Copy link

Meorge commented Jan 26, 2025

I like the underlying idea, but I'd personally be nervous about the specific syntax

{ item1, item2, item3 }

as that looks like it would be denoting an unordered set. It's the same syntax that Python uses for sets, and also how sets are often depicted in mathematics in general:

Image

(from https://www.mathsisfun.com/sets/symbols.html )

@sockeye-d
Copy link

I think I agree with Meorge, that kind of syntax would be nice to be reserved for a Set type

@Calinou Calinou changed the title Introduce Shorthand Syntax for Dictionary Keys in GDScript Add shorthand syntax for dictionary keys in GDScript Jan 27, 2025
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

4 participants