ek9_generate_delegation
Rewrites the class header's 'with trait of <Trait>' clause to '... by <field>' so the compiler forwards every trait method (no bodies generated); answers status, edits[] and summary.
When to use it
'Forward every Processor method of DelegatingProcessor to its inner field' is one call with symbol, traitName:'Processor' and delegate:'inner'.
When not to use it
- The class does NOT yet declare the trait at all - use
ek9_adopt_traitwith delegateFieldName, which appends 'with trait of <Trait> by <field>' in one step. - The delegate field doesn't exist yet - add it first with
ek9_add_field, then call this. - You want stub bodies to fill in rather than compiler-generated forwarding - use
ek9_implement_methods(mode=stubs). - You want to override ONE trait method's behaviour - wire delegation here first, then
ek9_add_methodfor the explicit override (it shadows the forwarded method).
Details
Modifies ONE line - the class declaration - because 'by' attaches to the trait reference at class-declaration time, not per-method (per-method 'name by field' syntax does not exist). status:'error' for validation failures: target not a class, class declares no traits, trait not declared on the class, ambiguous trait name, delegate field missing, trait already delegated, or a malformed class-header line (the grammar keeps the whole header on one physical line, so a multi-line clause cannot occur). A non-conforming delegate field type is NOT blocked - the tool proceeds and notes it; a genuine mismatch surfaces as status:'would-fail' from the whole-workspace verify (never 'conflicts'; no banned-identifier check since the field already exists).
Arguments
| Argument | Type | Required | What it is |
|---|---|---|---|
delegate | string | yes | The name of an existing field on the class to delegate to. Its declared type should implement the trait (the compile gate confirms). |
file | string | The EK9 source file - OPTIONAL when symbol is a construct name; required to anchor on a member or a use site. | |
line | integer | 1-based line of the declaration or a reference - OMIT it and pass symbol:'<name>' to let the tool find the line (a model counting lines in a file miscounts); if you do count, use ek9_read_file(lineNumbers:true). | |
symbol | string | yes | The class that declares the trait - a bare name ('DelegatingProcessor') finds its file and declaration workspace-wide; 'mod::Name' when the name is declared in two modules. |
traitName | string | yes | The trait already declared on the class via 'with trait of'. Simple name (Processor) when unambiguous; fully-qualified (com.acme::Processor) when needed. |
It also takes the arguments every tool takes.
Recorded calls
Real calls, recorded against small projects and checked on every build - a changed answer fails it. 3 of the 4 recorded are shown.
Delegate field missing
Request
{
"delegate" : "nope",
"file" : "processors.ek9",
"line" : 16,
"traitName" : "Processor"
}
Response
{
"conflicts" : [ ],
"edits" : [ ],
"error" : {
"message" : "delegate field 'nope' not found on class 'DelegatingProcessor'"
},
"meta" : {
"serverMs" : "<timing>"
},
"status" : "error",
"summary" : "delegate field 'nope' not found on class 'DelegatingProcessor'",
"wouldBeDiagnostics" : [ ],
"written" : false
}
Non class target
Request
{
"delegate" : "delegate",
"file" : "processors.ek9",
"line" : 6,
"traitName" : "Processor"
}
Response
{
"conflicts" : [ ],
"edits" : [ ],
"error" : {
"message" : "'Processor' is a trait - `with trait of X by field` applies to a class only"
},
"meta" : {
"serverMs" : "<timing>"
},
"status" : "error",
"summary" : "'Processor' is a trait - `with trait of X by field` applies to a class only",
"wouldBeDiagnostics" : [ ],
"written" : false
}
Trait not declared
Request
{
"delegate" : "delegate",
"file" : "processors.ek9",
"line" : 21,
"traitName" : "Processor"
}
Response
{
"conflicts" : [ ],
"edits" : [ ],
"error" : {
"message" : "Class 'NoTraitClass' does not declare trait 'Processor' in its header — add the trait declaration first"
},
"meta" : {
"serverMs" : "<timing>"
},
"status" : "error",
"summary" : "Class 'NoTraitClass' does not declare trait 'Processor' in its header — add the trait declaration first",
"wouldBeDiagnostics" : [ ],
"written" : false
}
See also
- Show me in the IDE: Generate Delegation