E11018: unused captured variable in dynamic function or class
What went wrong
A variable is captured by a closure/dynamic function but never used inside that function's body.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 1338 (Why does EK9 report E11018 for an unused captured vari...)
How to fix it
- Remove the unused variable from enclosing scope if not needed
- Or use the variable in the dynamic function body
- Or restructure to avoid capturing what's not needed
Common causes
- Copy-paste error - captured variable from template not needed
- Refactoring removed usage but left capture
- Variable intended for use but forgotten
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E11018: unused captured variable in dynamic function or class
#!ek9 defines module error.e11018.bad defines program ShowUnusedCapture() stdout <- Stdout() //'second' is captured but the hand-written 'operator ?' only checks 'first'. pair <- Pair(first: "hello", second: "world") as class override operator ? as pure <- rtn as Boolean: first? stdout.println(`${pair?}`)
Fixed: Use every captured value - here the generated operator ? checks them all
#!ek9 defines module error.e11018.good defines program ShowPositive() stdout <- Stdout() //Both captures are read implicitly by the synthesised 'default operator ?'. pair <- Pair(first: "hello", second: "world") as class default operator ? stdout.println(`${pair?}`)
Questions and answers
These Q&A entries show E11018 and its fix:
Found a problem?
If EK9 reported E11018 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.