[swift-evolution] idea: immutable setters for structs and tuples?

Brent Royal-Gordon brent at architechies.com
Thu Mar 24 16:39:29 CDT 2016


>> I think this is better modeled in Swift as something like:
>> 
>> 	let john = (firstName:"John", lastName:"Doe")
>> 	let alice = with(john) {
>> 		$0.firstName = "Alice"
>> 	}
> 
> You can kind of do this now:
> 
> struct Person {
>     var firstName, lastName: String
> }
> 
> func modify<T>(item: T, update: (inout T) -> Void) -> T {
>     var this = item
>     update(&this)
>     return this
> }
> 
> let carol: Person = modify(john) {
>     $0.firstName = "Carol"
> }
> 
> print(carol)

You *can* do this now. I'm suggesting it be added to Stdlib.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list