tact-lang / tact-docs

Tact main documentation
https://docs.tact-lang.org
58 stars 39 forks source link

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

Open jeshecdom opened 3 months ago

jeshecdom commented 3 months ago

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).