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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion doc/classes/Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

[codeblocks]
[gdscript]
var array = ["First", 2, 3, "Last"]
Expand All @@ -15,6 +15,10 @@
array[1] = "Second"
print(array[1]) # Prints "Second"
print(array[-3]) # Prints "Second"

# This array can only contain integers.
# Attempting to add any other type will result in an error.
var typed_array: Array[int] = [1, 2, 3]
[/gdscript]
[csharp]
Godot.Collections.Array array = ["First", 2, 3, "Last"];
Expand All @@ -25,6 +29,10 @@
array[1] = "Second";
GD.Print(array[1]); // Prints "Second"
GD.Print(array[^3]); // Prints "Second"

// This array can only contain integers.
// Attempting to add any other type will result in an error.
Godot.Collections.Array&lt;int&gt; 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.

[/csharp]
[/codeblocks]
[b]Note:[/b] Arrays are always passed by [b]reference[/b]. To get a copy of an array that can be modified independently of the original array, use [method duplicate].
Expand Down
35 changes: 34 additions & 1 deletion doc/classes/Dictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
pointsDict["Blue"] = 150; // Add "Blue" as a key and assign 150 as its value.
[/csharp]
[/codeblocks]
Finally, dictionaries can contain different types of keys and values in the same dictionary:
Finally, untyped dictionaries can contain different types of keys and values in the same dictionary:
[codeblocks]
[gdscript]
# This is a valid dictionary.
Expand Down Expand Up @@ -133,6 +133,39 @@
}
[/csharp]
[/codeblocks]
To enforce a certain type for keys and values, you can create a [i]typed dictionary[/i]. Typed dictionaries can only contain keys and values of the given types, or that inherit from the given classes:
[codeblocks]
[gdscript]
# Creates a typed dictionary with String keys and int values.
# 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

}

# Creates a typed dictionary with String keys and values of any type.
# 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",

}
[/gdscript]
[csharp]
// Creates a typed dictionary with String keys and int values.
// 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},

};

// Creates a typed dictionary with String keys and values of any type.
// 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"},

};
[/csharp]
[/codeblocks]
[b]Note:[/b] Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use [method duplicate].
[b]Note:[/b] Erasing elements while iterating over dictionaries is [b]not[/b] supported and will result in unpredictable behavior.
</description>
Expand Down