Skip to content

Document typed dictionaries and arrays in the class reference #107071

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

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

Conversation

Calinou
Copy link
Member

@Calinou Calinou commented Jun 3, 2025

@Calinou Calinou requested a review from a team as a code owner June 3, 2025 00:23
@Calinou Calinou added enhancement documentation cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release labels Jun 3, 2025
@Calinou Calinou added this to the 4.5 milestone Jun 3, 2025

// This array can only contain integers.
// Attempting to add any other type will result in an error.
Godot.Collections.Array<int> typedArray = [1, 2, 3];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this valid C# syntax, or do we need to go the long way and use new Godot.Collections.Array for the righthand side?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Godot.Collections.Array implements IEnumerable and has an Add method with an appropriate signature, so it should be able to support this syntax (which is called "collection expressions". added in C# 12).

Although this is a somewhat recent addition to C#, so it might be better to avoid using it.

@Mickeon
Copy link
Member

Mickeon commented Jun 3, 2025

@@ -4,7 +4,7 @@
A built-in data structure that holds a sequence of elements.
</brief_description>
<description>
An array data structure that can contain a sequence of elements of any [Variant] type. Elements are accessed by a numerical index starting at [code]0[/code]. Negative indices are used to count from the back ([code]-1[/code] is the last element, [code]-2[/code] is the second to last, etc.).
An array data structure that can contain a sequence of elements of any [Variant] type (untyped array), or a specific type (typed array). Elements are accessed by a numerical index starting at [code]0[/code]. Negative indices are used to count from the back ([code]-1[/code] is the last element, [code]-2[/code] is the second to last, etc.).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't look more in-depth as of writing this, but I would not add these parenthesis. Rather, I'd expand briefly on what a typed array is with a new sentence.

# Attempting to use any other type for keys or values will result in an error.
var typed_dict: Dictionary[String, int] = {
"some_key": 1,
"some_other_key": 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"some_other_key": 2
"some_other_key": 2,

Per the style guide

# Attempting to use any other type for keys will result in an error.
var typed_dict_key_only: Dictionary[String, Variant] = {
"some_key": 12.34,
"some_other_key": "string"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"some_other_key": "string"
"some_other_key": "string",

// Attempting to use any other type for keys or values will result in an error.
var typedDict = new Godot.Collections.Dictionary&lt;String, int&gt; {
{"some_key", 1},
{"some_other_key", 2}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{"some_other_key", 2}
{"some_other_key", 2},

// Attempting to use any other type for keys will result in an error.
var typedDictKeyOnly = new Godot.Collections.Dictionary&lt;String, Variant&gt; {
{"some_key", 12.34},
{"some_other_key", "string"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{"some_other_key", "string"}
{"some_other_key", "string"},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release documentation enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add documentation about Typed Dictionaries
5 participants