<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 10, 2016 at 5:41 PM, Brent Royal-Gordon <span dir="ltr">&lt;<a href="mailto:brent@architechies.com" target="_blank">brent@architechies.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">&gt; I&#39;m curious what the type signature of the splat operator would be?<br>
<br>
</span>None of the options I present actually involves a standalone operator; even 3 is a magic syntax which happens to look like an operator. That&#39;s why you can use the splat without specifying a tuple to get a tuple-taking version of the function.<br>
<br></blockquote><div><br></div><div>Ah, I see, at least for #1.  I&#39;m assuming that to get the function that takes a tuple, you&#39;d have to explicitly use the parameters: overload:</div><div><br></div><div>let f = concatenate   // Gives you concatenate(_, to:), type signature (Int, to: String) -&gt; String</div><div>f(1, to: &quot;foo&quot;)  // Legal</div><div>f(2, &quot;foo&quot;)  // Legal, argument labels may be omitted</div><div>f(3, from: &quot;foo&quot;)  // Illegal, argument labels don&#39;t line up</div><div><br></div><div>let g = concatenate(parameters:) // Gives you concatenate(parameters), type signature (Int, String) -&gt; String</div><div>g(1, to: &quot;foo&quot;) // Legal, tuple label ignored</div><div>g(2, &quot;foo&quot;)  // Legal</div><div>g(3, bar: &quot;foo&quot;)  // Legal, tuple label ignored</div><div><br></div><div>For #2, I&#39;m asking what the type is of .apply(to:)?</div><div><br></div><div>let f = concatenate(_:to:).apply(to:)  // I assume this has type (Int, String) -&gt; String</div><div>var f = concatenate</div><div>var g = f.apply(to:)  // Is this legal?  What&#39;s the type?</div><div><br></div><div>class Promise&lt;ResultTuple&gt; {</div><div>  init(resolve: ResultTuple -&gt; Void) {</div><div>    self.resolve = resolve</div><div>  }</div><div><br></div><div>  func onPromiseComplete(result: ResultTuple) {</div><div>    resolve.apply(to: result)  // Is this legal?</div><div>  }</div><div><br></div><div>  let resolve: ResultTuple -&gt; Void</div><div>}</div><div><br></div><div>let p1 = Promise(resolve: concatenate)  // Presumably this is a Promise&lt;(Int, String)&gt;</div><div>let p2 = Promise(resolve: f)  // What&#39;s the type of this?  Compiler smart enough to figure out it&#39;s Promise&lt;(Int, String)&gt;?</div><div>let p3 = Promise(resolve: g)  // How about this?</div><div>let p4 = Promise(resolve: Promise(resolve:))   // For extra recursion...</div><div><br></div><div>// This type signature is wrong, g takes an arbitrary argument list...but how do I write that?</div><div>func compose&lt;Params, Intermediate, Result&gt;(f: Intermediate -&gt; Result , g: Params -&gt; Intermediate) -&gt; Result {<br></div><div>  return { args in f.apply(to: g.apply(to: args)) }</div><div>}</div><div><br></div><div>// Here&#39;s a function that takes an Array of functions and applies them one after another...</div><div>// I don&#39;t really know where to start with this...the types of all of its arguments seem inexpressible</div><div>func composeMany&lt;/* ...arbitrary many intermediate params... */&gt;(functions: [/* What type for arbitrary function? */]) {</div><div>  reduce(compose, functions, identity)</div><div>}</div><div><br></div><div>A lot of this is going overboard with functional programming and is probably outside the scope of what Swift&#39;s designers want it to accomplish.  My point isn&#39;t to turn Swift into Haskell, it&#39;s to point out some of the potential edge cases when interacting with other language features.   Many of those edge cases can be avoided by saying &quot;This is a simple syntactic feature, it *looks* like this but all it does is construct a closure that&#39;s syntactic sugar for applying this function to each of the components of its tuple-based argument.&quot;  But then if it looks like a method, people are going to wonder why they can&#39;t pass it to a function or store it in a data-structure.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
I *did* consider adding a fourth option, that of annotating parameters as converting n-ary functions to tuple-taking equivalents, but I didn&#39;t feel like I had a good enough idea of how that would work to really suggest it. Roughly, an `apply(_:to:)` function would look something like this:<br>
<br>
        func apply&lt;In, Out&gt;(function: @splatting In -&gt; Out, to params: In) -&gt; Out {<br>
                // `function` is of type `In -&gt; Out`, where In is a tuple containing the types<br>
                // of the parameters of the function that was passed. Essentially, the generic system<br>
                // implicitly wraps the function in a trampoline that takes a tuple of compatible arguments.<br>
                return function(params)<br>
        }<br>
<br>
But this starts getting deep into type checker and generic system territory, which I don&#39;t understand well enough to feel comfortable proposing.<br>
<span class=""><br>
&gt; If it can&#39;t, I have a strong preference against the options (#1 &amp; #2) that look like normal function call syntax.  Because you won&#39;t be able to do several things that you&#39;re accustomed to with functions: assign them to variables, store them in containers, pass them as parameters to other functions.<br>
<br>
</span>I think this feature has to be able to make a first-class, tuple-taking closure, and all three alternatives I presented are intended to do that. That&#39;s what the second example for each syntax (the one where I map it over an array of tuples) is meant to show.<br></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">
&gt; This is less of a problem with explicit language syntax, because you could have a rule in the typechecker that says &quot;If the expression being splatted is a tuple of type (A, B, x: C), then it must be applied to a function of type (A, B, C) -&gt; Ret, with the result type of the call being Ret.&quot;  I think you can also get around many of the type inference pitfalls as well, because in most cases the types of the function and tuple are unlikely to need inferring (occasionally this will require explicit type annotations on tuple components, but it seems like most of the time they will have already been inferred when the tuple was declared).<br>
<br>
</span>As long as the splatting is explicit—that is, the parser can tell whether you&#39;re making a normal call or a tuple call—I don&#39;t think the overload resolution on a splatted version of a function is any more difficult than the non-splatted version. Possibly even easier, depending on how we handle default arguments.<br>
<span class=""><br>
&gt; And it&#39;s much easier to do &quot;partial splat&quot; operations, where, for example, you may want to pass the first couple arguments of a function explicitly but splat the rest.<br>
<br>
</span>I haven&#39;t really considered how to do this kind of thing, but I think it&#39;s probably better represented by constructing a single tuple containing all of the arguments. I believe there was another thread recently that discussed tuple combining operators.<br><span class=""><font color="#888888"><br></font></span></blockquote><div><br></div><div>I saw that, briefly.  I hope that something like that makes it in, but it has similar issues with &quot;What&#39;s the type signature of this operator, and if the only way it can exist is as compiler-supported syntax, how do we make it clear to users that this is a compiler language feature and not a first-class function call?&quot;  IIRC the syntax suggested there made use of the #thisIsACompilerDirective naming convention.  I wonder if that might be appropriate here:</div><div><br></div><div>f(#splat(tuple))</div><div>concatenate(_: #splat)</div><div><br></div><div>The latter form also suggests a way this could be used for partial application:</div><div><br></div><div>concatenate(2, #splat) returns a closure where the first argument is always 2, but any remaining arguments are pulled from the provided tuple.</div></div><br></div></div>