godotengine / godotengine/godot
Typed Array/Dictionary incompatible with ternary assignment
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
Reproducible in: 4.5.stable
### System information
Godot v4.5.stable - Ubuntu 24.04.3 LTS 24.04 on Wayland - X11 display driver, Multi-window, 1 monitor - OpenGL 3 (Compatibility) - AMD Radeon 760M (radeonsi, phoenix, LLVM 20.1.2, DRM 3.57, 6.8.0-79-generic) - AMD Ryzen 5 7640U w/ Radeon 760M Graphics (12 threads) - 13.45 GiB memory
### Issue description
You cannot define types of Arrays/Dictionaries if they are assigned with a ternary expression.
If you just define an Array/Dictionary without value, then assign a value in each conditional branch, it works.
Shouldn't these methods be equal?
The error is only shown at runtime, it expects just a plain Array/Dictionary:
`Trying to assign a dictionary of type "Dictionary" to a variable of type "Dictionary[String, int]".`
Edit: If you type cast both values, it works with a typed Array/Dictionary but that seems very unnecessary.
### Steps to reproduce
```GDScript
var condition: bool = true
# does not work, errors at runtime
# but does work with untyped Dictionary
var data_ternary: Dictionary[String, int] = {
old = 0,
new = 1,
} if condition else {
old = 8,
new = 9,
}
# does work
var data: Dictionary[String, int] = {}
if condition:
data.old = 0
data.new = 1
else:
data.old = 8
data.new = 9
```
### Minimal reproduction project (MRP)
N/A
Contributor guide
Research direction
Start by running the provided GDScript reproduction in Godot v4.5.stable and compare the ternary assignment with the branch-based assignment. Done when the typed Dictionary assignment accepts both branches without the reported runtime error, while preserving the working untyped and explicit-cast cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100