modify
helps support in-place mutation, which can help ARC optimizations. Consider the following code:
foo.x.append(0) // `foo` is an instance of `Foo` from Joe Groff's example
If Foo.x
were to use get
/set
, then x
would return a new Array
instance referencing the contents of _x
. append(0)
would then mutate that array, triggering a copy-on-write, then that array would be written back to _x
.
Since Foo.x
uses modify
, x
will yield _x
's Array
instance so that append(0)
can directly mutate _x
. If _x
's backing storage is known to be uniquely referenced and there is capacity for a new element, then this will avoid a copy-on-write.
IIRC this is why modify
is already used for Array
's subscript (using _modify
since it's not yet a stable feature).
You may also want to read this proposal for more information about modify
and yield
.