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

Maximilian Hünenberger m.huenenberger at me.com
Sun Mar 27 09:30:12 CDT 2016


> Am 24.03.2016 um 22:40 schrieb Erica Sadun via swift-evolution <swift-evolution at swift.org>:
> 
> 
>> On Mar 24, 2016, at 3:39 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
>> 
>>>> 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.
> 
> Ah, then that's a "pass it by Dmitri" kind of thing, isn't it?
> 
> -- E

I think so. Although there should be a formal proposal.
Regarding the new naming conventions shouldn't it be "modified"?

Kind regards
- Maximilian

> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution


More information about the swift-evolution mailing list