Why can't I use 'with application of' on a regular class method in EK9?
← Classes and OOP · Ref: Q838
The 'with application of' syntax is only valid on programs — it tells the EK9 runtime which application context to inject dependencies from. Using it on a regular class method is an error because class methods do not participate in application-level dependency injection.
THE RULE
- Programs CAN use 'with application of <name>' to select a DI context
- Classes, records, functions, and components CANNOT use this syntax
- Application selection is a program-level concern, not a method-level concern
WHY ONLY PROGRAMS
Programs are entry points — they are the top of the dependency injection graph. The 'with application of' annotation tells the runtime which registered application provides the component bindings. Regular methods receive their dependencies through constructor injection or component fields, not through application selection.
THIS EXAMPLE
Shows a program using 'with application of' to select an application context. The Logger component receives its dependency through the DI container, not through application selection on methods.
See Q111 for components. See Q227 for compile-time DI. See Q231 for program-application linking.
Example
defines module qa.classesandoop.application.selection defines component Logger prefix <- "INFO" default Logger() Logger() -> p as String prefix :=: p log() -> message as String stdout <- Stdout() stdout.println(`[${prefix}] ${message}`) default operator ? defines class Processor name <- "default" default Processor() Processor() -> n as String name :=: n process() <- rtn as String: `Processed by ${name}` default operator ? defines program <?- Programs are entry points and CAN use 'with application of'. This is where DI context selection belongs. -?> ProcessorDemo() stdout <- Stdout() processor <- Processor("main") if processor? stdout.println(processor.process()) logger <- Logger("DEBUG") if logger? logger.log("Application started")
Common mistakes
E07360 — The 'with application of' syntax is only valid on programs, not on class methods. Programs are entry points for DI. Class methods receive dependencies through constructor injection. See ek9 -h E07360 for details.
Incorrect:
process() with application of myApp <- rtn as String: `Processed by ${name}`
Correct:
process()
<- rtn as String: `Processed by ${name}`
E07360 — 'with application of' cannot be used on component methods. Application selection belongs on programs only. See ek9 -h E07360 for details.
Incorrect:
log() with application of myApp -> message as String
Correct:
log()
-> message as String
Other ways to ask this
- What is E07360 APPLICATION_SELECTION_INVALID?
- Where can I use 'with application of' in EK9?
- How does application selection work in EK9 programs?
Coming from another language?
Java Spring: @SpringBootApplication on the main class, not on methods. Python Django: DJANGO_SETTINGS_MODULE at application level. Rust: no DI framework in language. Kotlin: Dagger @Component on application class. Go: wire at application level. EK9: 'with application of' on programs only — class methods use constructor injection.
Keywords: E07360, context, method, component, selection, entry, dependency, program, application, injection