attribute [nonmutating]¶
Description¶
Marks a function or a property and subscript accessor as non-mutating. A non-mutating function receives the implicit this parameter as an in parameter, so mutations to members accessed from this argument will be prohibited by the compiler.
Signature¶
[nonmutating]
Remarks¶
Member functions of a type are non-mutating by default, so this attribute is not necessary in most cases. However, the set accessor of a property or subscript is mutating by default, and you can use [nonmutating] to mark it as non-mutating. For example:
struct S
{
int* ptr_x;
property x : int
{
get { return *ptr_x; }
[nonmutating]
set { *ptr_x = value; }
}
}
uniform S s; // `s` is not mutable.
void test() { s.x = 1; } // OK, because the `set` accessor is non-mutating.
In the above example, the property x reads and writes to a memory location pointed to by ptr_x. Therefore, the set accessor is not actually modifying any field of S, and does not need to take this as an inout parameter. Using [nonmutating] here on the set accessor will allow it to be called with a non-mutating value of S.
See also¶
[mutating].