<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="">In Scala you can implement an apply method which makes it possible to call an object just like a function. Example:<div class=""><br class=""></div><div class="">case class Foo(x: Int) {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>def apply(y: Int) = x + y</div><div class="">}</div><div class=""><br class=""></div><div class="">val foo = Foo(3)</div><div class="">val bar = foo(4)</div><div class=""><br class=""></div><div class="">That is similar to what you suggested to have a possibility to convert an object to a closure getting called. And I totally see the point for this! I think using a keyword or special name like apply is not a good idea because it's not obvious what it does and it also makes it possible to just call the method with its name: foo.apply(4).</div><div class=""><br class=""></div><div class="">However, having a protocol is kinda hard because it's not possible to have a flexible parameter list. Maybe having a method without a name? Swift example:</div><div class=""><br class=""></div><div class=""><div class="">class Foo {</div><div class=""> var x: Int</div><div class=""> init(x: Int) { self.x = x }</div><div class=""> </div><div class=""> func (y: Int) -> Int {</div><div class=""> return self.x + y</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">let foo = Foo(x: 3)</div><div class="">let bar = foo(y: 4)</div><div class=""><br class=""></div><div class="">I actually like that, would be like an anonymous function. It would also be possible to have multiple of those defined for one object (which would have to be unambiguous of course).</div><div class=""><br class=""></div><div class="">So getting back to KeyPath, it could look like this:</div><div class=""><br class=""></div><div class=""><div class="">class KeyPath<Root, Value> {</div><div class=""> func (_ root: Root) -> Value {</div><div class=""> return root[keyPath: self]</div><div class=""> } </div><div class="">}</div></div><div class=""><br class=""></div><div class="">I see that this would be a much bigger change and would not justify the syntactic sugar for map, flatMap, etc. But it would still be a nice addition to the Swift programming language, especially for KeyPath, transformers etc.</div><div class=""><br class=""></div><div class="">What do you think?</div><div class="">
<div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">______________________<br class=""><br class="">Benjamin Herzog</div><div style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><br class=""></div><br class="Apple-interchange-newline">
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On 9. Jul 2017, at 13:09, Karl Wagner <<a href="mailto:razielim@gmail.com" class="">razielim@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><div id="edo-message" class=""><div class=""></div>I agree that it should be completely implicit.</div><div id="edo-message" class=""><br class=""></div><div id="edo-message" class="">KeyPaths are simply chains of partially-applied properties and subscripts. At the same time, it was noted in the KeyPath proposal that a similar mechanism might be used to model chains of partially-applied functions. I think that having both types be convertible to a closure would be sensible.</div><div id="edo-message" class=""><br class=""></div><div id="edo-message" class="">In fact, you could argue for a general-purpose “Executable” protocol which would allow any conforming object to be implicitly used as a function/closure. Command-style objects such as predictes and transformers would also benefit from such a feature.</div><div id="edo-message" class=""><br class=""></div><div id="edo-message" class="">- Karl</div><div id="edo-message" class=""><br class=""></div></div></div></blockquote></div><br class=""></div></body></html>