<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="">+1 on the idea.<div class=""><br class=""></div><div class="">I'm less convinced by the suggested syntax, to me it feels a bit weird.</div><div class=""><br class=""></div><div class="">I'm thinking an attribute on the closure's first parameter, for exemple (feel free to suggest other names):</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #35568a" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #35568a" class="">func</span> with<T>(value: <span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">T</span>, body: (<span style="font-variant-ligatures: no-common-ligatures; color: #35568a" class="">bound</span> <span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">T</span>) -> ()) -> <span style="font-variant-ligatures: no-common-ligatures; color: #c35900" class="">T</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> body(value)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #35568a" class="">return</span> value</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">Le 26 févr. 2016 à 08:22, Eugene Gubin via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">+1 very interesting idea</div><div class="gmail_extra"><br class=""><div class="gmail_quote">2016-02-26 9:09 GMT+03:00 Russ Bishop via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span>:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">+1 to this or something like it.<br class="">
<br class="">
Right now I’m relying on crap^H^H^H^Hstuff like dispatch_get_specific to provide that kind of context without introducing something like Quick’s global “World”.<br class="">
<br class="">
Russ<br class="">
<div class="HOEnZb"><div class="h5"><br class="">
> On Feb 25, 2016, at 2:47 PM, Stephen Celis via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">
><br class="">
> I'm reopening a topic that has already been discussed here at length[1]: setup closures and `self`-binding closures.<br class="">
><br class="">
> I've been wondering if Swift could adopt what Kotlin calls "extension function expressions"[2]. These would allow us to encode a closure's receiver type (`self`) into the closure type itself, providing a powerful, type-safe way to define DSLs.<br class="">
><br class="">
> Erica's earlier draft proposed this builder pattern:<br class="">
><br class="">
> with let task = NSTask() {<br class="">
> launchPath = "/usr/bin/mdfind"<br class="">
> arguments = ["kMDItemDisplayName == *.playground"]<br class="">
> standardOutput = pipe<br class="">
> }<br class="">
><br class="">
> With extension functions, we could approach something similar:<br class="">
><br class="">
> func with<T>(value: T, body: T.() -> ()) -> T {<br class="">
> value.body()<br class="">
> return value<br class="">
> }<br class="">
><br class="">
> let task = with(NSTask()) {<br class="">
> launchPath = "/usr/bin/mdfind"<br class="">
> arguments = ["kMDItemDisplayName == *.playground"]<br class="">
> standardOutput = pipe<br class="">
> }<br class="">
><br class="">
> Or, using the `then`[3] pattern:<br class="">
><br class="">
> protocol Builder {}<br class="">
> extension Builder {<br class="">
> func then(body: Self.() -> ()) -> Self {<br class="">
> body()<br class="">
> return self<br class="">
> }<br class="">
> }<br class="">
> extension NSTask: Builder {}<br class="">
><br class="">
> let task = NSTask().then {<br class="">
> launchPath = "/usr/bin/mdfind"<br class="">
> arguments = ["kMDItemDisplayName == *.playground"]<br class="">
> standardOutput = pipe<br class="">
> }<br class="">
><br class="">
> How about BDD-style frameworks (like Quick/Nimble[4] and Spectre[5])?<br class="">
><br class="">
> describe("a person") {<br class="">
> let person = Person(name: "Kyle")<br class="">
><br class="">
> it("has a name") {<br class="">
> try expect(<a href="http://person.name/" rel="noreferrer" target="_blank" class="">person.name</a>) == "Kyle"<br class="">
> }<br class="">
> }<br class="">
><br class="">
> And HTML builders?<br class="">
><br class="">
> html {<br class="">
> head { title("Hello, World!") }<br class="">
> }<br class="">
><br class="">
> And block-based, transactional APIs?<br class="">
><br class="">
> db.inTransaction {<br class="">
> delete("users", "first_name = ?", ["Jake"])<br class="">
> }<br class="">
><br class="">
> The main benefits have been previously discussed:<br class="">
><br class="">
> 1. The ability to remove noise ("$0" everywhere)<br class="">
> 2. The ability to avoid defining globals (in favor of a safer, scoped interface)<br class="">
><br class="">
> Would such an enhancement be feasible?<br class="">
><br class="">
> ---<br class="">
> Stephen<br class="">
><br class="">
> Footnotes:<br class="">
><br class="">
> [1] A probably-incomplete list:<br class="">
> - "Idea for enabling DSLs: bind to self in closures": <a href="https://lists.swift.org/pipermail/swift-evolution/2015-December/000114.html" rel="noreferrer" target="_blank" class="">https://lists.swift.org/pipermail/swift-evolution/2015-December/000114.html</a><br class="">
> - "Request for Discussion: Setup closures": <a href="https://lists.swift.org/pipermail/swift-evolution/2015-December/000211.html" rel="noreferrer" target="_blank" class="">https://lists.swift.org/pipermail/swift-evolution/2015-December/000211.html</a><br class="">
> - "Method cascading (was Re: Request for Discussion: Setup closures)": <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151130/000729.html" rel="noreferrer" target="_blank" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151130/000729.html</a><br class="">
> - "Fluent syntax (replacing void with a useful default return value)": <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/374" rel="noreferrer" target="_blank" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/374</a><br class="">
> - "Lexical scope statement (with .. do)": <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/1408" rel="noreferrer" target="_blank" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/1408</a><br class="">
> - "Scoped resources (like C# using statement)": <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/1641" rel="noreferrer" target="_blank" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/1641</a><br class="">
> - "Customized Inline Init Closure": <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/1946" rel="noreferrer" target="_blank" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/1946</a><br class="">
> - "Then Support": <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/2054" rel="noreferrer" target="_blank" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/2054</a><br class="">
> - "Draft proposal: multi-property assignment .= operator": <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/2960" rel="noreferrer" target="_blank" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/2960</a><br class="">
> - "Custom default names for arguments of closures": <a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/6969" rel="noreferrer" target="_blank" class="">http://thread.gmane.org/gmane.comp.lang.swift.evolution/6969</a><br class="">
> - <a href="https://bugs.swift.org/browse/SR-160" rel="noreferrer" target="_blank" class="">https://bugs.swift.org/browse/SR-160</a><br class="">
><br class="">
> [2] <a href="https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver" rel="noreferrer" target="_blank" class="">https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver</a><br class="">
> [3] <a href="https://github.com/devxoul/Then" rel="noreferrer" target="_blank" class="">https://github.com/devxoul/Then</a><br class="">
> [4] <a href="https://github.com/Quick/Quick" rel="noreferrer" target="_blank" class="">https://github.com/Quick/Quick</a><br class="">
> [5] <a href="https://github.com/kylef/Spectre" rel="noreferrer" target="_blank" class="">https://github.com/kylef/Spectre</a><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="">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><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="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div>
_______________________________________________<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></blockquote></div><br class=""></div></body></html>