<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>I still think using an operator for this conversation would neither increase readability nor transparency. I think my mail on Sunday was lost, so I paste the content here again. It referred to a suggestion to create a possibility for KeyPath to act as a function which would bring other benefits as well:<br></div>
<div><br></div>
<div>In Scala you can implement an apply method which makes it possible to call an object just like a function. Example:<br></div>
<div><br></div>
<div>case class Foo(x: Int) {<br></div>
<div> def apply(y: Int) = x + y<br></div>
<div>}<br></div>
<div><br></div>
<div>val foo = Foo(3)<br></div>
<div>val bar = foo(4) // 7<br></div>
<div><br></div>
<div>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).<br></div>
<div><br></div>
<div>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:<br></div>
<div><br></div>
<div>class Foo {<br></div>
<div> var x: Int<br></div>
<div> init(x: Int) { self.x = x }<br></div>
<div> <br></div>
<div> func (y: Int) -> Int {<br></div>
<div> return self.x + y<br></div>
<div> }<br></div>
<div>}<br></div>
<div><br></div>
<div>let foo = Foo(x: 3)<br></div>
<div>let bar = foo(y: 4) // 7<br></div>
<div><br></div>
<div>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).<br></div>
<div><br></div>
<div>So getting back to KeyPath, it could look like this:<br></div>
<div><br></div>
<div>class KeyPath<Root, Value> {<br></div>
<div> func (_ root: Root) -> Value {<br></div>
<div> return root[keyPath: self]<br></div>
<div> } <br></div>
<div>}<br></div>
<div><br></div>
<div>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.<br></div>
<div><br></div>
<div>What do you think?<br></div>
<div><br></div>
<div id="sig64302232"><div class="signature">______________________<br></div>
<div class="signature"><br></div>
<div class="signature">Benjamin Herzog<br></div>
</div>
<div><br></div>
</body>
</html>