[swift-evolution] [Pitch] Circling back to `with`

Brent Royal-Gordon brent at architechies.com
Fri May 27 17:12:43 CDT 2016


>> Just mentioning it as to end up... with the proper name for this new function.
> 
> Naming can always be bikeshedded. 

One possibility is to split `with` in two:

- A plain `with` whose closure parameter is not mutable and which is marked `@discardableResult`.

- A `withVar` whose parameter *is* mutable and which is *not* marked `@discardableResult`. (This would help with the fact that our use of `@discardableResult` is a little dangerous, in that people might expect mutations to affect the original variable even if it's a value type.)

`withVar` does, I think, make it pretty clear that you're working with a copy of the variable.

	/// Returns `item` after calling `use` to inspect it.
	/// 
	/// If `T` is a value type, `use` is unable to mutate `item`.
	/// If `T` is a reference type, `use` may use members which 
	/// change `item`, but cannot assign a different instance.
	@discardableResult
	public func with<T>(_ item: T, use: @noescape (T) throws -> Void) rethrows -> T {
	  try use(item)
	  return item
	}
	
	/// Returns `item` after calling `update` to inspect and possibly 
	/// modify it.
	/// 
	/// If `T` is a value type, `update` uses an independent copy 
	/// of `item`. If `T` is a reference type, `update` uses the 
	/// same instance passed in, but it can substitute a different 
	/// instance by setting its parameter to a new value.
	public func withVar<T>(_ item: T, update: @noescape (inout T) throws -> Void) rethrows -> T {
	  var this = item
	  try update(&this)
	  return this
	}

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list