E06070: type inference is not supported within generic/template type/functions
What went wrong
Type inference using '<-' cannot be used inside generic/template definitions. Generic code must use explicit type declarations because the compiler needs to know how types relate to the generic parameters.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 854 (Why can't I use type inference inside a generic class bod...)
How to fix it
- Replace the inferred declaration 'name <- value' with an explicit type: 'name as T: value'
- Use the generic type parameter (T, K, V, etc.) in declarations
- Be explicit about all variable types inside generic code
Rationale
Inside generics, the compiler must track how every type relates to the generic parameters. Inference could hide type relationships that matter during parameterization (e.g., when T becomes String).
Explicit declaration patterns
value as T: expression // T is the generic type parameter list as List of T: List() // Generic using generic type param maybe as Optional of T: Optional() // completed from the declared type
Common causes
- Using inference patterns from non-generic code
- Forgetting that generic context has stricter rules
- Assuming compiler can infer relationship to T
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E06070: type inference is not supported within generic/template type/functions
#!ek9 defines module error.e06070.bad defines class Box of type T item as T: T() default Box() Box() -> initial as T item: initial //Inside a generic type, every declaration states its type check() -> candidate as T <- rtn <- candidate? count() <- rtn as Integer? start <- 1 rtn: start default operator ?
Fixed: Declare the type explicitly inside a generic type or function
#!ek9 defines module error.e06070.good defines class Box of type T item as T: T() default Box() Box() -> initial as T item: initial check() -> candidate as T <- rtn as Boolean: candidate? count() <- rtn as Integer? start as Integer: 1 rtn: start default operator ?
Questions and answers
These Q&A entries show E06070 and its fix: