IMO with could be called a lot of things:
withwithCopy(of:)withMutableCopy(of:)mutatingMutableCopy(of:)
I picked with because it was brief and commonly used.
IMO with could be called a lot of things:
withwithCopy(of:)withMutableCopy(of:)mutatingMutableCopy(of:)I picked with because it was brief and commonly used.
Kotlin gives you a bunch of functions exactly because of slightly different semantics. Scope functions | Kotlin Documentation
And keep in mind they don't take into account whether or not they should produce a copy first.
Names are really the minorest of the issues to address.
What semantics issues do you see that arenât addressed by naming the function withMutableCopy(of:)?
The reason I didn't name it that was:
To be clear, I donât even think it classifies as a shallow copy. Usually âshallow copyâ implies a new object with a different object identity, but with any internal references unmodified.
oh
I tend to agree with Ben that there's insufficient value in sugaring the bound-mutation operation because it's too redundant with the local-inout binding we want to add anyway.
I could certainly be convinced that with is not a good name for the (copy)-mutate-return operation â it's so vague that it can only be understood as an idiom, so if the prior art isn't consistent with the idiom we want it to mean, maybe we ought to pick something else.
My point is that the design of this feature should take into account all semantics the language is capable of. It can't be achieved with just one function.
We should address the following:
It would also be wonderful if there will be a way to guarantee that the closure will be executed exactly once and in the same callstack.
I described it all in my proposal about do blocks, checkout the section 3.
Just because all of these semantics could potentially be useful doesn't mean they all need to be addressed by one proposal. This is a targeted addition for something that is quite useful
Yes, func with<T>(_: inout T, do: (inout T) -> Void) has an extremely trivial definition. But as I recall saying in a previous thread, I think it is useful because it brings the power of lexically-scoped borrowing to any current Swift programmer without them having to learn about consume.
func usingInoutVar() {
inout var item = myModel.items[selectedItemIndex]
item.name = newName
item.price = newPrice
item.upc = newUPC
_ = consume item // Needed to guarantee that borrow doesnât extend past here
print("updated model")
}
func usingMutateInPlace() {
with(myModel.items[selectedItemIndex]) { item in
item.name = newName
item.price = newPrice
item.upc = newUPC
} // borrow implicitly ends here
print("updated model")
}
And as this thread has been discussing recently, a mutate-in-place method incurs no ambiguity about what it means to âcopyâ a reference.
As Ben pointed out, thereâs already a spelling for copying a value type and changing its properties:
var newValue = oldValue
newValue.foo = "yello"
newValue.bar = 42
whereas there is not currently a spelling for mutating a value in place. I see much more value in the latter, and not a lot of value in the former. (Though Iâm starting to think it should be called apply to steer clear of the two competing meanings of with.)
While it's true that they can be implemented separately, still I feel like they should be designed together to be coherent. Even if it will be decided to leave some of them out it should be an explicit and considered decision.
I feel like there is a spelling for mutating a value in place it would just be:
oldValue.foo = "hello"
oldValue.bar = 42
you would just directly mutate it
Not if itâs stored within another object. In-place mutation of local variables is trivial.
Maybe this?
{ prop in
// do stuff with `prop`
}(&someProperty.that.is.very.nested)
OK, strictly speaking it was incorrect for me to say thereâs no spelling for in-place mutation
But this construction is definitely not as obvious in its intent as the assignment operator, and it is backwards by putting the property access expression at the end.
That could be done but I think is different from with. Maybe modify?
someProperty.that.is.very.nested.modify { prop in
}
Of course, just like with this is pretty trivial to implement.
public func modify<T, U>(_ value: inout T, transform: (inout T) throws -> U) rethrows -> U {
return try transform(&value)
}
And in fact, since it requires no new features, I think that if itâs added to the standard library, it should be @alwaysEmitIntoClient.
The language steering group guidance on this is as follows:
Lastly, a variation of this feature that takes its argument
inouthas also been proposed. We'd avoid this sort of design so as not to expose idiomatic ways to "hide" mutations in a chain of operations. The situation is analogous to assignment operators, which do not return a value in Swift (unlike most C-family languages). Moreover, anyinoutversion of the proposed feature would require the user already to have a mutable binding "on hand" that they could mutate directly, and we expectinoutbindings to address use cases where one might want to repeatedly mutate some deeply nested member.