E07895: cannot assign to a method-call result - not an assignable target; only mutating operators (+=, -=, :~:, :^:, :=:) apply to a call result
What went wrong
You're trying to assign to the RESULT of a method call. A call result is a temporary value, not a storage location, so there is nothing for a simple (:=) or guarded (:=?) assignment to store into.
A mutating operator IS allowed on a call result, because it modifies the returned object in place rather than storing into a slot: 'holder.current() += 1' compiles, while 'holder.current() := 99' has no location to assign into (both are in the [EXAMPLE] section below).
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 1363 (Can I apply a mutating operator like += to a method-call ...)
How to fix it
- To mutate the returned object, use a mutating operator (+=, -=, :~:, :^:, :=:) rather than an assignment
- If you meant to replace what the method returns, add a setter method and call it: repository.setItems(List())
- If you want to work on the returned value, take it into a variable first and work on that variable
Common causes
- Using := / :=? where a mutating operator (+=, :~:, ...) was intended
- Treating a getter's result as an assignable field
- Expecting an accessor to behave like a settable property
See also
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E07895: cannot assign to a method-call result - not an assignable target; only mutating operators (+=, -=, :~:, :^:, :=:) apply to a call result
#!ek9 defines module error.e07895.bad defines class Holder count <- 0 current() <- rtn as Integer: count default operator ? defines function assignToCallResult() -> holder as Holder //A call result is a value, not somewhere to store one holder.current() := 99
Fixed: Mutate it in place, or assign it to a variable first
#!ek9 defines module error.e07895.good defines class Holder count <- 0 current() <- rtn as Integer: count default operator ? defines function useCallResult() -> holder as Holder <- rtn as Integer: 0 //Mutate the returned object in place with a mutating operator holder.current() += 1 //Or take it into a variable and work on that rtn: holder.current() rtn += 99
Questions and answers
These Q&A entries show E07895 and its fix:
Found a problem?
If EK9 reported E07895 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.