mirror of
https://github.com/godotengine/godot.git
synced 2025-11-01 06:01:14 +00:00
Add typed arrays to GDScript
- Use `Array[type]` for type-hints. e.g.: `var array: Array[int] = [1, 2, 3]` - Array literals are typed if their storage is typed (variable asssignment of as argument in function all). Otherwise they are untyped.
This commit is contained in:
parent
997a8ae9e8
commit
85e316a5d5
12 changed files with 569 additions and 58 deletions
|
|
@ -2674,6 +2674,19 @@ GDScriptParser::TypeNode *GDScriptParser::parse_type(bool p_allow_void) {
|
|||
|
||||
type->type_chain.push_back(type_element);
|
||||
|
||||
if (match(GDScriptTokenizer::Token::BRACKET_OPEN)) {
|
||||
// Typed collection (like Array[int]).
|
||||
type->container_type = parse_type(false); // Don't allow void for array element type.
|
||||
if (type->container_type == nullptr) {
|
||||
push_error(R"(Expected type for collection after "[".)");
|
||||
type = nullptr;
|
||||
} else if (type->container_type->container_type != nullptr) {
|
||||
push_error("Nested typed collections are not supported.");
|
||||
}
|
||||
consume(GDScriptTokenizer::Token::BRACKET_CLOSE, R"(Expected closing "]" after collection type.)");
|
||||
return type;
|
||||
}
|
||||
|
||||
int chain_index = 1;
|
||||
while (match(GDScriptTokenizer::Token::PERIOD)) {
|
||||
make_completion_context(COMPLETION_TYPE_ATTRIBUTE, type, chain_index++);
|
||||
|
|
@ -3278,6 +3291,9 @@ String GDScriptParser::DataType::to_string() const {
|
|||
if (builtin_type == Variant::NIL) {
|
||||
return "null";
|
||||
}
|
||||
if (builtin_type == Variant::ARRAY && has_container_element_type()) {
|
||||
return vformat("Array[%s]", container_element_type->to_string());
|
||||
}
|
||||
return Variant::get_type_name(builtin_type);
|
||||
case NATIVE:
|
||||
if (is_meta_type) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue