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

Tyler Cloutier cloutiertyler at aol.com
Wed Mar 23 15:41:42 CDT 2016


I would very much like a standardized way of doing this. I am currently writing a game implementation in a functional style and the syntax for updating fields in pretty clunky. I’d like to avoid having to explicitly use var’s at all to accomplish it.


> On Mar 23, 2016, at 7:29 AM, James Campbell via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Yeah a way to have a function that mutates a struct return a new copy with that mutation would be awesome ! Rather than mutating in place.
> 
> ___________________________________
> 
> James⎥Head Of CEO
> 
> james at supmenow.com <mailto:james at supmenow.com>⎥supmenow.com <http://supmenow.com/>
> Sup
> 
> Runway East
> 
> 
> 10 Finsbury Square
> 
> London
> 
> 
> EC2A 1AF 
> 
> 
> On Wed, Mar 23, 2016 at 2:28 PM, William Dillon via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> I like this.  I’m always annoyed when I need var just because I can’t get all my initialization done with let.
> 
> - Will
> 
> > On Mar 23, 2016, at 2:32 AM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> >
> >> let john = {firstName="John"; lastName="Doe"}
> >> let alice = {john with FirstName="Alice"}
> >>
> >>  Current way to do this in Swift is:
> >>
> >> let john = (firstName:"John", lastName:"Doe")
> >> var alice = john
> >> alice.firstName = "Alice"
> >
> > I think this is better modeled in Swift as something like:
> >
> >       let john = (firstName:"John", lastName:"Doe")
> >       let alice = with(john) {
> >               $0.firstName = "Alice"
> >       }
> >
> > `with` would be something like:
> >
> >       func with<Value>(value: Value, function: Value throws -> Void) rethrows -> Value {
> >               var mutableValue = value
> >               return try function(&mutableValue)
> >       }
> >
> > This would serve many different purposes:
> >
> > * If the value is a value type, allows you to return a modified copy
> > * Allows you to customize a value's properties immediately after initializing it, which many people have asked for
> > * Acts as a `tap` function when the block doesn't change the value (see <http://ruby-doc.org/core-2.3.0/Object.html#method-i-tap <http://ruby-doc.org/core-2.3.0/Object.html#method-i-tap>>)
> >
> > --
> > Brent Royal-Gordon
> > Architechies
> >
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> > https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160323/cd2af82f/attachment.html>


More information about the swift-evolution mailing list