tact-docs icon indicating copy to clipboard operation
tact-docs copied to clipboard

Clarify behavior of `mutates` functions when applied on a constant

Open jeshecdom opened this issue 6 months ago • 0 comments

The description of a mutates function specifies that it can change the argument given in the self parameter.

For example, given the declaration,

let A: Int = 0;

and the mutates function,

extends mutates fun inc(self: Int) {
   self += 1;
}

the statement,

A.inc();

will increase variable A.

However, if A was declared as a constant,

const A: Int = 0;

the statement,

A.inc();

will NOT increase A, and Tact will silently allow such attempt of changing A, contrary to expectation.

The reason for Tact silently allowing this, is that Tact carries out constant substitution. This means that A.inc() is actually 0.inc() during compilation. Hence, no actual attempt of changing A exists (and executing 0.inc() has the same effect as incrementing a variable local to inc initialized with 0). This silent behavior between constants and mutates functions should be documented in the description of mutates functions (for example, adding a note in https://docs.tact-lang.org/book/functions#mutable-functions).

jeshecdom avatar Aug 10 '24 07:08 jeshecdom