[swift-evolution] Splat
Andrew Bennett
cacoyi at gmail.com
Wed Feb 10 20:06:11 CST 2016
I like the first option, concatenate(parameters:)
It may not have issues conflicting if "parameters" was renamed to something
which is an invalid label, this would also make it clearer that it's doing
something special. For example:
concatenate(#parameters: tuple)
It may be that it can also distinguish different labels if the label is
stored as part of the type information. If I remember correctly there is a
bug which would allow this when fixed.
For example:
concatenate(#parameters: (123, with: "string"))
I'm fairly sure this should be sufficient, even in generics, the tuple just
needs to have the labels.
It may also be nice to allow unlabelled tuples if the function is
unambiguous.
The thread on tuples combining operators was probably this one:
Tuple conversion and type composition
http://comments.gmane.org/gmane.comp.lang.swift.evolution/5334
On Thursday, 11 February 2016, Brent Royal-Gordon via swift-evolution <
swift-evolution at swift.org
<javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>> wrote:
> > I'm curious what the type signature of the splat operator would be?
>
> 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's why
> you can use the splat without specifying a tuple to get a tuple-taking
> version of the function.
>
> I *did* consider adding a fourth option, that of annotating parameters as
> converting n-ary functions to tuple-taking equivalents, but I didn'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:
>
> func apply<In, Out>(function: @splatting In -> Out, to params: In)
> -> Out {
> // `function` is of type `In -> Out`, where In is a tuple
> containing the types
> // of the parameters of the function that was passed.
> Essentially, the generic system
> // implicitly wraps the function in a trampoline that
> takes a tuple of compatible arguments.
> return function(params)
> }
>
> But this starts getting deep into type checker and generic system
> territory, which I don't understand well enough to feel comfortable
> proposing.
>
> > If it can't, I have a strong preference against the options (#1 & #2)
> that look like normal function call syntax. Because you won't be able to
> do several things that you're accustomed to with functions: assign them to
> variables, store them in containers, pass them as parameters to other
> functions.
>
> 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's what the second example for each syntax (the one where I map it over
> an array of tuples) is meant to show.
>
> > This is less of a problem with explicit language syntax, because you
> could have a rule in the typechecker that says "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) -> Ret, with the result type of the call being
> Ret." 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).
>
> As long as the splatting is explicit—that is, the parser can tell whether
> you're making a normal call or a tuple call—I don'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.
>
> > And it's much easier to do "partial splat" operations, where, for
> example, you may want to pass the first couple arguments of a function
> explicitly but splat the rest.
>
> I haven't really considered how to do this kind of thing, but I think it'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.
>
> --
> Brent Royal-Gordon
> Architechies
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160211/619d95de/attachment.html>
More information about the swift-evolution
mailing list