E04050: type must be a simple aggregate/list/dict
What went wrong
Type inference cannot resolve the type from a complex expression. When using '<-' for declaration, the compiler needs to infer the type BEFORE full expression processing. Complex expressions require explicit typing.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 104 (Why does EK9 require explicit constructors for uninitiali...)
What can be inferred
- Simple construction: x <- SomeType()
- Parameterized types: x <- List() of String
- List literals: x <- [1, 2, 3]
- Dict literals: x <- {"key": "value"}
What cannot be inferred
- Arithmetic: x <- getValue() + 10
- Method chains: x <- obj.process().transform()
- Complex expressions: x <- condition ? valueA : valueB
How to fix it
Use explicit type declaration for complex expressions:
variableName as Type: complexExpression
Rationale
Phase 2 must resolve all property types BEFORE Phase 3 expression processing. Complex expressions require the full type system, creating a circular dependency. Explicit typing breaks this cycle.
See also
- E04060: not an aggregate type
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E04050: type must be a simple aggregate/list/dict
#!ek9 defines module error.e04050.bad defines function firstName() //A method call cannot give '<-' its type: the compiler infers it before methods are resolved <- name <- List("Steve").getOrDefault(0, "")
Fixed: Declare the type and assign the call, instead of inferring the type from a method call
#!ek9 defines module error.e04050.good.fix defines function firstName() //A method call cannot give '<-' its type, so the type is written <- name as String: List("Steve").getOrDefault(0, "")
Fixed: Declare the type, then assign the expression - or infer only from a literal or constructor
#!ek9 defines module error.e04050.good defines function firstName() <- name as String: List("Steve").getOrDefault(0, "") emptyCount() <- count <- Integer() names() <- rtn <- ["Steve", "Stephen", "Steven"]
Questions and answers
These Q&A entries show E04050 and its fix:
- Q104: Why does EK9 require explicit constructors for uninitialised properties?
- Q585: How do field initialization and constructor delegation interact?
- Q1282: What does EK9 error E04050 mean when I declare a class field and how do I fix it?
- Q1283: When should I use 'name as Type: value' instead of 'name <- value' for a class field to avoid E04050?
Found a problem?
If EK9 reported E04050 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.