Skip to content

Commit 4fcd855

Browse files
committed
Merge pull request #107071 from Calinou/doc-typed-dictionary-array
Document typed dictionaries and arrays in the class reference
2 parents 369ed51 + b6dcb11 commit 4fcd855

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

doc/classes/Array.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
A built-in data structure that holds a sequence of elements.
55
</brief_description>
66
<description>
7-
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.).
7+
An array data structure that can contain a sequence of elements of any [Variant] type by default. Values can optionally be constrained to a specific type by creating a [i]typed array[/i]. 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.).
88
[codeblocks]
99
[gdscript]
1010
var array = ["First", 2, 3, "Last"]
@@ -15,6 +15,10 @@
1515
array[1] = "Second"
1616
print(array[1]) # Prints "Second"
1717
print(array[-3]) # Prints "Second"
18+
19+
# This typed array can only contain integers.
20+
# Attempting to add any other type will result in an error.
21+
var typed_array: Array[int] = [1, 2, 3]
1822
[/gdscript]
1923
[csharp]
2024
Godot.Collections.Array array = ["First", 2, 3, "Last"];
@@ -25,6 +29,10 @@
2529
array[1] = "Second";
2630
GD.Print(array[1]); // Prints "Second"
2731
GD.Print(array[^3]); // Prints "Second"
32+
33+
// This typed array can only contain integers.
34+
// Attempting to add any other type will result in an error.
35+
Godot.Collections.Array&gt;int&lt; typedArray = [1, 2, 3];
2836
[/csharp]
2937
[/codeblocks]
3038
[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].

doc/classes/Dictionary.xml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
pointsDict["Blue"] = 150; // Add "Blue" as a key and assign 150 as its value.
9595
[/csharp]
9696
[/codeblocks]
97-
Finally, dictionaries can contain different types of keys and values in the same dictionary:
97+
Finally, untyped dictionaries can contain different types of keys and values in the same dictionary:
9898
[codeblocks]
9999
[gdscript]
100100
# This is a valid dictionary.
@@ -133,6 +133,39 @@
133133
}
134134
[/csharp]
135135
[/codeblocks]
136+
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:
137+
[codeblocks]
138+
[gdscript]
139+
# Creates a typed dictionary with String keys and int values.
140+
# Attempting to use any other type for keys or values will result in an error.
141+
var typed_dict: Dictionary[String, int] = {
142+
"some_key": 1,
143+
"some_other_key": 2,
144+
}
145+
146+
# Creates a typed dictionary with String keys and values of any type.
147+
# Attempting to use any other type for keys will result in an error.
148+
var typed_dict_key_only: Dictionary[String, Variant] = {
149+
"some_key": 12.34,
150+
"some_other_key": "string",
151+
}
152+
[/gdscript]
153+
[csharp]
154+
// Creates a typed dictionary with String keys and int values.
155+
// Attempting to use any other type for keys or values will result in an error.
156+
var typedDict = new Godot.Collections.Dictionary&lt;String, int&gt; {
157+
{"some_key", 1},
158+
{"some_other_key", 2},
159+
};
160+
161+
// Creates a typed dictionary with String keys and values of any type.
162+
// Attempting to use any other type for keys will result in an error.
163+
var typedDictKeyOnly = new Godot.Collections.Dictionary&lt;String, Variant&gt; {
164+
{"some_key", 12.34},
165+
{"some_other_key", "string"},
166+
};
167+
[/csharp]
168+
[/codeblocks]
136169
[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].
137170
[b]Note:[/b] Erasing elements while iterating over dictionaries is [b]not[/b] supported and will result in unpredictable behavior.
138171
</description>

0 commit comments

Comments
 (0)