Twitter | Search | |
Noah Gilmore
Swift WritableKeyPaths are so dope. Just wrote this super simple protocol you can put on any struct to make a new copy with only one property changed
Reply Retweet Like More
Jesse Squires Feb 4
Replying to @noahsark769
properties have to be `var` correct?
Reply Retweet Like
Noah Gilmore Feb 4
Replying to @jesse_squires
Yup, this is the one drawback imo. Interestingly, doesn't work if they're private(set) either
Reply Retweet Like
πš–πšŠπš›πšπš’-πšœπšžπš£πšžπš”πš’ Feb 4
Replying to @noahsark769
i've tried something interesting with dynamicMemberLookup😎
Reply Retweet Like
Noah Gilmore Feb 4
Replying to @marty_suzuki
Oh yeah dynamicMemberLookup makes it suuuuuuper slick syntax. I wrote a more complicated implementation with lenses today which I’m going to write about at some point
Reply Retweet Like
David Feb 6
Nice! Sad that you hace to make them var instead of let. Any ideas on how to make it work without var?
Reply Retweet Like
Noah Gilmore Feb 6
I don’t think so, unfortunately - at least not without code gen
Reply Retweet Like
Thomas Grapperon Feb 4
The use cases are also similar to 's withable:
Reply Retweet Like
Noah Gilmore Feb 4
Yep, same idea!
Reply Retweet Like
Jordan Kay 😷 Feb 4
Replying to @noahsark769 @b3ll
Is this advantageous to use over let book = Book(title: "Something", author: "Noah", isbn: 1, publishedYear: 2020) var updated = book updated.title = "Else" ?
Reply Retweet Like
Noah Gilmore Feb 4
Replying to @_Jordan @b3ll
Great question - with one level deep it's basically just syntactic sugar (though it's nice to not have the extra var lying around as others have mentioned), but it's even better when you have to go two levels deep:
Reply Retweet Like