E08160: dependency injection is not allowed
What went wrong
This injection ('!') cannot be made. Injection may be written in a component, service, class, trait, function, dynamic class or dynamic function - as a property or inside a method or function body - and in a program linked to an application. It is refused here because of one of:
- the type is not a component (a class, record, trait, function or built-in type cannot be injected - only an abstract component can)
- the variable is an incoming parameter or a returning value
- the injection is inside a record (a record holds data only: its fields take part in its generated equality, hashing and copying)
- the type is a generic type parameter
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 957 (Why can I only inject components, not classes, in EK9?)
- ek9 -q 668 (Which constructs support component injection?)
How to fix it
- Inject an abstract component: 'logger as Logger!' where Logger is an abstract component
- For a parameter or return value, pass the component in or out as an ordinary value, and inject it where it is first needed
- Move an injection out of a record into the class, component or function that uses the record
Common causes
- Injecting a class, record or built-in type instead of a component
- Marking a parameter or return value with '!'
- Adding an injected property to a record
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E08160: dependency injection is not allowed
#!ek9 defines module error.e08160.bad defines function report() <- rtn as String? title as String! rtn: title
Fixed: Inject into a component, never a function: take the value as a parameter instead
#!ek9 defines module error.e08160.good.fix defines function report() -> title as String <- rtn as String? rtn: title
Fixed: Only an abstract component is injected
#!ek9 defines module error.e08160.good defines function report() <- rtn as String? timekeeper as Timekeeper! rtn: timekeeper.now() defines component Timekeeper as abstract now() as pure abstract <- rtn as String?
Questions and answers
These Q&A entries show E08160 and its fix:
Found a problem?
If EK9 reported E08160 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.