E08280: a dynamic function never re-points a variable it captured - a capture is an initial value (read it, and declare a new variable for a new value) or a way back out (copy into it with ':=:', merge with ':~:' or replace with ':^:', which the caller sees); re-pointing changes only the function's own name
What went wrong
Inside a dynamic function, a variable it captured was assigned with ':', ':=', '=' or ':=?'. That re-points only the function's own name for the capture: the object the caller holds is not touched, so the value is never seen - not by the caller, and not by anything the caller passed that object to.
Rationale
A capture shares the caller's OBJECT, not the caller's variable. It has two jobs, and re-pointing does neither:
- an initial value: the function reads it;
- a way back out: the function changes the shared object in place, and the caller sees the change.
':=?' is a re-point too: it assigns a new object when the capture is unset, so the caller's object stays unset. And re-pointing first makes any later in-place change go into the new object, so it is lost as well.
How to fix it
- To pass a result out, copy it INTO the captured object with ':=:' (or merge with ':~:', replace with ':^:'); inside a MutexKey this is the only way a result leaves the lock (the [EXAMPLE] section below shows the re-point, and the copy)
- To fill the captured object only when it is unset, guard it and change it in place: 'if not value?' and then 'value :^: fallback'
- To work with a different value inside the function, declare a new variable from the capture with '?:' or a ternary, and only read the capture itself
- If the function keeps state of its own between calls, change the capture in place ('+=', '++'), or hold the state in a dynamic class, whose captures are fields its methods may assign
Common causes
- Returning a result from a MutexKey with ':' instead of ':=:', which points the key's own name for the return variable at a new Boolean.
- Setting a flag such as 'found: true' inside a function passed to a stream or a callback, expecting the caller's 'found' to change.
- Writing 'value :=? fallback' to default a capture, which leaves the caller's object unset.
- Treating a capture as a local variable and giving it a new value.
What stays legal
- Reading a capture, and calling its methods.
- ':=:', ':~:', ':^:', '+=', '-=', '++' and the other in-place operators.
- A captured value escaping: added to a collection the caller passed in, stored, returned or handed to another call.
- Assigning a capture inside a dynamic CLASS: there it is a field, and the class's methods change it as ordinary object state.
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E08280: a dynamic function never re-points a variable it captured - a capture is an initial value (read it, and declare a new variable for a new value) or a way back out (copy into it with ':=:', merge with ':~:' or replace with ':^:', which the caller sees); re-pointing changes only the function's own name
#!ek9 defines module error.e08280.bad defines class Account lockedBalance as MutexLock of Integer: MutexLock(100) hasFunds() -> amount as Integer <- rtn as Boolean: false checker <- (amount, rtn) is MutexKey of Integer as function rtn: lockedItem >= amount require lockedBalance.enter(checker) default operator ?
Fixed: Read the capture and declare a new variable for a new value
#!ek9 defines module error.e08280.good defines function Greeter() as abstract <- rtn as String? greeterFor() -> name as String <- rtn as Greeter? rtn: (name) is Greeter as function shown <- name ?: "stranger" rtn: `Hello, ${shown}`
Fixed: Copy the answer into the Boolean the caller holds with ':=:'
#!ek9 defines module error.e08280.good.fix defines class Account lockedBalance as MutexLock of Integer: MutexLock(100) hasFunds() -> amount as Integer <- rtn as Boolean: false checker <- (amount, rtn) is MutexKey of Integer as function rtn :=: lockedItem >= amount require lockedBalance.enter(checker) default operator ?
Found a problem?
If EK9 reported E08280 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.