E07040: 'by' variable is not supported on a trait only on a class
What went wrong
You're using 'by' delegation syntax in a TRAIT, but delegation only works in classes.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 770 (Why can't I use 'by' delegation inside a trait?)
How to fix it
- Use delegation in a class instead
- Traits define interfaces only
- Classes provide implementations via delegation
Common causes
- Treating trait like a class
- Trying to delegate trait implementation within trait
- Misunderstanding what traits can contain
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E07040: 'by' variable is not supported on a trait only on a class
#!ek9 defines module error.e07040.bad defines trait Reader read() <- rtn as String: "read" Writer write() -> text as String <- rtn as Boolean: text? //A trait has no properties, so it has nothing to delegate 'by' ReaderWriter with trait of Reader by reader, Writer
Fixed: Delegate 'by' a property in a class
#!ek9 defines module error.e07040.good defines trait Reader read() as abstract <- rtn as String? defines class FileReader with trait of Reader override read() <- rtn as String: "read from file" //A class can delegate a trait 'by' one of its properties Document with trait of Reader by reader reader as Reader: FileReader() default operator ?
Questions and answers
These Q&A entries show E07040 and its fix:
Found a problem?
If EK9 reported E07040 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.