<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class="">Note that function like this :<br class="">func with<T>(item:T, apply:(T)->Void) { apply(item) }<br class=""><br class="">Produces such kind of problems:<br class="">struct A {var x = 1}<br class="">let a1 = A() // constant<br class="">with (a1) { $0.x = 10 } // this will be compiled without errors/warnings<br class=""></blockquote></div><div class=""><br class=""></div>This works as expected (giving a compile error for let constant):<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(131, 148, 150);" class=""><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// for value types</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> with<T>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">inout</span><span style="font-variant-ligatures: no-common-ligatures" class=""> item: </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">, apply: (</span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">inout</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Void</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">rethrows</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Void</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class=""> apply(&item)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// for reference types (classes)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> with<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">AnyObject</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(item: </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">, apply: (</span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">Void</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">rethrows</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class=""> apply(item)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> item</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// value types</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> A { </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> x = </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> a1 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">A</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">with</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (&</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">a1</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">x</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">10</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">a1</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// A(x: 10)</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">/*</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">let a2 = A()</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">with (&a2) { // error: cannot pass immutable value as inout argument: 'a2' is a 'let' constant</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> $0.x = 10</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">print(a2)</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">*/</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// reference types (classes)</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(181, 137, 1);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">class</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class=""> B : </span><span style="font-variant-ligatures: no-common-ligatures" class="">CustomDebugStringConvertible</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class=""> {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> y = </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">1</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> debugDescription: </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">"B(y: </span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">y</span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">))"</span><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> b1 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">B</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">with</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">b1</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">y</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">11</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">b1</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// B(y: 11)</span></div><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// reference types allow this pattern (value types don't)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #859901" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> b2 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">with</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: #b58901" class="">B</span><span style="font-variant-ligatures: no-common-ligatures" class="">()) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">y</span><span style="font-variant-ligatures: no-common-ligatures" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #29a198" class="">12</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; color: rgb(88, 110, 117);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">b2</span><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class="">) </span><span style="font-variant-ligatures: no-common-ligatures" class="">// B(y: 12)</span></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">-Thorsten</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #839496" class=""><br class=""></span></div><div><blockquote type="cite" class=""><div class="">Am 22.04.2016 um 09:18 schrieb Vladimir.S via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:</div><br class="Apple-interchange-newline"><div class=""><div class="">Just wanted to summarize our opinions on this suggestion that we was discussing earlier and check if someone is ready to crate an "official" proposal for this feature.<br class=""><br class="">There a number of questions regarding this feature we can discuss, for example statement vs method, what could be a placeholder of target instance inside the scope($0, $, _, .., nothing etc)<br class=""><br class="">The main question, *do you support that we need "with" feature in some way in Swift 3.0 out of the box* . And if so, what variant do you prefer.<br class=""><br class="">* this proposal is for *explicit* "with", where it is clear what method/property belongs to the "target" instance<br class="">* I believe that as such feature is really useful and handy, if it is explicit and if it is clearly showing in code what we are doing - we want to have this feature as part of language/standard lib rather than possibility to use some workaround to implement it<br class="">* It is not about saving the space in code. It is about more readable and (I insist) more stable(with less errors) code. Much less possibilities for copy-paste errors. Wrong code completion suggestion(by editor) can not produce error. It is explicit and clear, it has much less noise in code.<br class="">* Many of us already implemented and use such "with" construction in some way<br class=""><br class="">There were 2 main suggestions :<br class=""><br class="">1) Introduce "with" statement that can be used in for example in such way:<br class=""><br class="">// set props just after creating<br class="">// similar to "if let.. " and "guard let.."<br class="">with let questionLabel = UILabel() {<br class=""> //set props of created instance here<br class=""> // here we can have:<br class=""> // $0.prop = value<br class=""> // or<br class=""> // ..prop = value<br class=""> // or<br class=""> // .prop = value<br class=""> // or<br class=""> // _.prop = value<br class=""> // or<br class=""> // $.prop = value<br class=""> // or ?<br class="">}<br class="">// questionLabel is available here<br class=""><br class=""><br class="">// works for structures<br class="">with var some = SomeStruct() {<br class=""> //...<br class="">}<br class=""><br class="">// just for some class/structure/enum<br class="">with questionLabel {<br class=""> // ..<br class="">}<br class=""><br class="">probably<br class=""><br class="">with var src = someNamedInstance1,<br class=""> let dst = someNamedInstance2 {<br class=""> src.propA = dst.propB<br class=""> dst.someMethod(src.propC)<br class=""> src.someMehtod()<br class="">}<br class=""><br class="">or<br class="">with someNamedInstance1, someNamedInstance2 {<br class=""> $0.propA = $1.propB<br class=""> $1.someMethod($0.propC)<br class=""> $0.someMehtod()<br class="">}<br class=""><br class=""><br class="">2) Introduce .with method for each(?) class/struct, so we can use out-of-box:<br class=""><br class="">let questionLabel = UILabel().with {<br class=""> //set props of created instance here<br class=""> $0.prop = value<br class="">}<br class=""><br class="">var someStructInstance = SomeStruct().with {target in<br class=""> target.prop = value<br class="">}<br class=""><br class="">questionLabel.with {label in<br class=""> label.prop = value<br class="">}<br class=""><br class="">someNamedInstance1.with(someNamedInstance2) {src, dst in<br class=""> src.propA = dst.propB<br class=""> dst.someMethod(src.propC)<br class=""> src.someMehtod()<br class="">}<br class=""><br class="">Note that function like this :<br class="">func with<T>(item:T, apply:(T)->Void) { apply(item) }<br class=""><br class="">Produces such kind of problems:<br class="">struct A {var x = 1}<br class="">let a1 = A() // constant<br class="">with (a1) { $0.x = 10 } // this will be compiled without errors/warnings<br class=""><br class=""><br class="">On 13.04.2016 17:17, Radosław Pietruszewski via swift-evolution wrote:<br class=""><blockquote type="cite" class="">It can be (more-or-less) solved in library code today:<br class=""><br class=""> extension NSObjectProtocol {<br class=""> public func with(@noescape fn: Self -> Void) -> Self {<br class=""> fn(self)<br class=""> return self<br class=""> }<br class=""> }<br class=""><br class=""><br class="">This way, you can do, on NSObjects:<br class=""><br class=""> textLabel.with {<br class=""><br class=""> $0.textAlignment = .Left<br class=""><br class=""> $0.textColor = .darkTextColor()<br class=""><br class=""> }<br class=""><br class=""><br class="">I love this pattern.<br class=""><br class="">You can also make it a function to make it work with any value of any kind<br class="">(it will then take form of `with(foo) { …}`).<br class=""><br class="">Ideally, if you could write a universal extension (something like<br class="">`extension Any`), you could just add this behavior, with method syntax, to<br class="">everything.<br class=""><br class="">— Radek<br class=""><br class=""><blockquote type="cite" class="">On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution<br class=""><<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a> <<a href="mailto:swift-evolution@swift.org" class="">mailto:swift-evolution@swift.org</a>>> wrote:<br class=""><br class="">I recently learned some VBA and I found a very conveniently `with` statement.<br class=""><br class="">`with` statement can be helpful to set property for UIKit instance.<br class=""><br class="">for instance a UILabel instance `textLabel` ,with `with` statement we can<br class="">set UILabel property like this<br class=""><br class=""><br class="">```swift<br class=""><br class="">with textLabel{<br class=""><br class="">.textAlignment= .Left<br class=""><br class="">.textColor= UIColor.darkTextColor()<br class=""><br class="">.font= UIFont.systemFontOfSize(15)<br class=""><br class="">}<br class=""><br class="">```<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a> <<a href="mailto:swift-evolution@swift.org" class="">mailto:swift-evolution@swift.org</a>><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></blockquote><br class=""><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""><br class=""></blockquote>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></body></html>