[Pitch] `With` functions in the standard library

IMO with could be called a lot of things:

  • with
  • withCopy(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:

  1. The length of the name makes it a lot less useful
  2. This is a bit unclear for classes, where it is not really making a deep copy, just a shallow copy.

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.

2 Likes

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.

4 Likes

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:

  1. Copy?
  • Yes
  • No
  1. Type of modifier
  • Mutating
  • Consuming
  • Borrowing
  1. Result
  • No result
  • Result of transformation (defined by the closure)
  • Original value (or copy).

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.

1 Like

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.

2 Likes

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.

1 Like

Maybe this?

{ prop in
 // do stuff with `prop`
}(&someProperty.that.is.very.nested)
2 Likes

OK, strictly speaking it was incorrect for me to say there’s no spelling for in-place mutation :slightly_smiling_face: 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.

1 Like

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 inout has 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, any inout version of the proposed feature would require the user already to have a mutable binding "on hand" that they could mutate directly, and we expect inout bindings to address use cases where one might want to repeatedly mutate some deeply nested member.

3 Likes