E08140: component injection not allowed when scope is marked as 'pure'
What went wrong
You're trying to inject a COMPONENT inside a PURE context. Component injection is an impure operation (it involves runtime state).
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 666 (Why can't I inject components in a pure context?)
Rationale
Components represent stateful resources - not compatible with purity.
How to fix it
- Inject at field level instead of inside pure method
- Pass required data as parameters to pure method
- Remove 'pure' if component access needed
Common causes
- Component injection in pure method
- Pure function trying to access services
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E08140: component injection not allowed when scope is marked as 'pure'
#!ek9 defines module error.e08140.bad defines function stamp() as pure <- rtn as String? timekeeper as Timekeeper! rtn: timekeeper.now() defines component Timekeeper as abstract now() as pure abstract <- rtn as String?
Fixed: Inject only where the scope is not pure
#!ek9 defines module error.e08140.good defines function stamp() <- rtn as String? timekeeper as Timekeeper! rtn: timekeeper.now() defines component Timekeeper as abstract now() as pure abstract <- rtn as String?
Fixed: Or keep the function pure and pass the component in
#!ek9 defines module error.e08140.good.parameter defines component Timekeeper as abstract now() as pure abstract <- rtn as String? defines function stamp() as pure -> timekeeper as Timekeeper <- rtn as String: timekeeper.now()
Questions and answers
These Q&A entries show E08140 and its fix: