Skip to content

Document typed dictionaries in GDScript #10747

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 Mar 5, 2025

This is copied (and adapted) from the related array documentation.

@Calinou Calinou added enhancement topic:gdscript area:manual Issues and PRs related to the Manual/Tutorials section of the documentation cherrypick:4.4 labels Mar 5, 2025
Typed dictionaries have the syntax ``Array[KeyType, ValueType]``, where ``KeyType`` and ``ValueType``
can be any ``Variant`` type, native or user class, or enum. Both the key and value type **must** be specified,
but you can use ``Variant`` to make either of them untyped.
Nested dictionary types (like ``Dictionary[String, Dictionary[String, int]]``)
Copy link
Member

Choose a reason for hiding this comment

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

The error message thrown says Nested typed collections are not supported., this includes both Dictionary and Array.
It could be misunderstood as "Dictionary" as a type cant be used, instead of typed dictionary.

Suggested change
Nested dictionary types (like ``Dictionary[String, Dictionary[String, int]]``)
Nested typed collections (like ``Dictionary[String, Dictionary[String, int]]``)

The wording in the typed array section should probably be updated, too.
same with static_typing.rst line 242

keys or values. The GDScript static analyzer takes typed dictionaries into account. However,
dictionary methods that return values still have the ``Variant`` return type.

Typed dictionaries have the syntax ``Array[KeyType, ValueType]``, where ``KeyType`` and ``ValueType``
Copy link
Contributor

@idbrii idbrii Mar 10, 2025

Choose a reason for hiding this comment

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

Based on the other files it looks like Array is a typo here. Should it be:

Typed dictionaries have the syntax ``Dictionary[KeyType, ValueType]``

A dictionary's value type applies to ``for`` loop variables, as well as some operators like
``[]`` and ``[]=``. Dictionary methods that return values and other operators
(such as ``==``) are still untyped. Built-in types, native and custom classes,
and enums may be used as element types. Nested array types
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
and enums may be used as element types. Nested array types
and enums may be used as element types. Nested dictionary types

var vehicles: Dictionary[String, Node] = { "car": $Car, "plane": $Plane }
var item_tiles: Dictionary[Vector2i, Item] = { Vector2i(0, 0): Item.new(), Vector2i(0, 1): Item.new() }
var dictionary_of_dictionaries: Dictionary[String, Dictionary] = { { } }
# var arrays: Dictionary[String, Dictionary[String, int]] -- disallowed
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# var arrays: Dictionary[String, Dictionary[String, int]] -- disallowed
# var dicts: Dictionary[String, Dictionary[String, int]] -- disallowed

Copy link
Contributor

@idbrii idbrii left a comment

Choose a reason for hiding this comment

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

Some lingering "array" look like they should be replaced with "dictionary".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:manual Issues and PRs related to the Manual/Tutorials section of the documentation cherrypick:4.4 enhancement topic:gdscript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants