attribute [mutating]¶
Description¶
Mark a function or a property or subscript accessor as mutating. A mutating function receives the implicit this parameter as an inout parameter, so that mutations to members access from this argument will be visible to the caller.
Signature¶
[mutating]
Remarks¶
By default, Slang treats all member functions as non-mutating. For example, consider the following function:
struct S
{
int x;
void foo()
{
x = 1; // error: `x` is not an l-value.
}
}
The line x=1 will lead to a compile time error because by-default, all member methods in Slang are non-mutating. To allow foo to modify x, you can use [mutating] to mark the function as such:
struct S
{
int x;
[mutating]
void foo()
{
x = 1; // ok
}
}
See also¶
[nonmutating].