-
-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
base: master
Are you sure you want to change the base?
Conversation
Calinou
commented
Jun 3, 2025
- This closes Add documentation about Typed Dictionaries godot-docs#10981.
|
||
// 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]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Hold on. See also: |
@@ -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.). |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"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<String, int> { | ||
{"some_key", 1}, | ||
{"some_other_key", 2} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{"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<String, Variant> { | ||
{"some_key", 12.34}, | ||
{"some_other_key", "string"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{"some_other_key", "string"} | |
{"some_other_key", "string"}, |