Why is my alias of a property that I only change with '++' or '+=' reported as not referenced (E08090)?

← Data Flow Safety · Ref: Q2275

'<-' binds a reference, never a copy. So 't <- counter.total' makes t the very Integer the record holds, and 't += 5' or 't++' changes counter.total in place: the change DOES happen. What EK9 rejects is the long way round - an alias that is only ever changed and never read has a direct form, and that is what to write (E08090):

  t <- counter.total
  t++

The alias is never read, so the compiler asks for the direct form. The fix is the change on the property:

  counter.total += 1

'++' and '--' count exactly as '+= 1' and '-= 1': a write is not a use, whichever operator makes it. Read the alias afterwards and it is used (it was needed as a name), so keep it only when you do.

A call's result is different. 't <- order.getTotal()' may hand back part of the order, so changing t changes what the order holds and counts as a use; when the call returns a fresh value, changing it is seen by nothing and is E08090 as before. See Q789 for unused variables and Q905 for mutating operators and aliasing.

Example

#!ek9
defines module qa.dataflowsafety.propertyaliasonlychanged

  defines record

    Counter
      total as Integer: Integer(0)

      default operator ?

  defines function

    bump()
      -> counter as Counter
      counter.total += 1

  defines program

    ShowBump()
      stdout <- Stdout()
      counter <- Counter()
      bump(counter)
      bump(counter)
      stdout.println(`total: ${counter.total}`)

Common mistakes

E08090 — '<-' binds the same Integer the property holds, so the alias changes counter.total in place but is never read. Change the property directly. See ek9 -h E08090 for details.

Incorrect:

      alias <- counter.total
      alias++

Correct:

      counter.total += 1
Other ways to ask this
  • Why does 't <- counter.total' then 't++' give E08090?
  • Does changing an alias of a property change the property?
  • How do I increment a property through a local variable?
  • Why is a variable I only increment reported as unused?

Coming from another language?

Java: Integer is immutable, so 'int t = counter.total; t++' changes only the local copy and the field is untouched. Python: 't = counter.total; t += 1' rebinds t; the attribute is untouched. EK9: '<-' binds the same object, and Integer's '++' and '+=' change it in place, so the alias and the property move together - write 'counter.total += 1' instead.

Keywords: ++, +=, E08090, alias, increment, mutating operator, not referenced, property, reference