[swift-evolution] Checking in; more thoughts on arrays and variadic generics

Daryle Walker darylew at mac.com
Thu Jan 26 12:37:20 CST 2017


> On Jan 23, 2017, at 3:33 PM, Robert Widmann <devteam.codafi at gmail.com> wrote:
> 
> Some thoughts inline.
> 
>> On Jan 21, 2017, at 11:06 AM, Daryle Walker via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> 1. Variadic generics
>> 
>> When I look at SwiftDoc.org <http://swiftdoc.org/>, I see some functions repeated with differing numbers of parameters. This seems like a job for variadic templates^H^H^H^H^H^H^H^H^H generics, like in C++. Fortunately, someone has already wrote about this, at <https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#variadic-generics <https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#variadic-generics>>. A new idea I came up with is that both homogeneous (the current support) and heterogeneous variadic parameters can appear in function declarations. Each can appear at most once in a declaration. And they can co-exist in the same declaration; there’s no problem unless the two packs are adjacent and at least the (lexically) second one doesn’t mandate a label. In that case, and when the homogenous pack appears second, count from the lexically last argument backwards until an argument cannot be part of the homogeneous type, that’ll be the border. Count the other way when the homogenous pack is first. (It’s possible for one pack to have zero elements.)
> 
> C++ has a simpler rule (for once): If you’re going to pack, you have to pack last.  This is roughly the rule we have as well for argument lists in functions that don’t have labels - they can have any number of variadic parameters because we can use the argument label to guide the tuple type comparison and disambiguate.  Here we lack argument labels (and I’m not sure it’s useful to have them).  
> 
> As for the distinction between heterogeneous and homogenous lists, I’m not sure it’s a useful thing to have unless you’re trying to roll your own Tuple (which is a thing you can do now with HLists <https://github.com/typelift/Swiftz/blob/master/Sources/HList.swift> anyway).  Any type that wishes to take a variadic number of homogeneous type variables is a type that can be parametrized by one type variable and enforce the cardinality invariant elsewhere (see std::initializer_list).
> 

The “homogenous list” I’m talking about are the variadic parameters that are already in the language. And they can already be non-last in the list. (At least it compiled, but the parameter I had after it had a default value, so I don’t know if that made a difference.) The “homogenous” is to differentiate them from parameters introduced by variadic generics (which would be “heterogeneous”).

My inspiration was from C++, where a function template can have both C++ and C variadic parameters. The C-level ones have to be last, and the C++-level ones next-to-last. Last I checked, this pattern was only useful for stupid resolution tricks; you couldn’t actually use such a function. (Using C-level variadics requires using a macro on the preceding argument. But on a dual-variadic function, this would be the C++-level variadic argument (instead of a singular one), which was (is?) not compatible with said macro.) I just want to make a generic function that uses both kinds of variadic argument lists actually usable, by defining the dividing line. Again, this is only a problem if the two lists are adjacent and the second list doesn’t have an external label.

AFAIK, there is no concrete specification for variadic generic functions yet. Hopefully, that team will take my advice into account. IF you don’t think a heterogeneous list should be non-last, nor co-exist with homogenous list, you can bring it up then.

>> 2. More on Arrays
>> 
[SNIP]
> 
> This also doesn’t seem to fit with the rest of the language.  To my mind a more correct answer is, once again C++-style, integers-in-parameter-position and a catch-all homogenous `Tuple<T, .UInt>` (more than likely, magic type alias).

Right now, generics can’t use non-type parameters, so any “FixedArray<Int, 6>”-like syntax is out. I feel that anonymous arrays would have a usage model mostly like tuples; hence the close syntax. I prefer a built-in type instead of a library type since we need some magic to enforce:

	strideof( ArrayType ) == Element-Count * strideof( ElementType )

down to the extent that there shouldn't be any padding between elements of the inner non-array type (in the case of nested arrays).

Since we anonymous and named heterogenous product types (tuples vs. structs and classes), I want the same thing for homogenous product types (array extension to tuples vs. nominal “array" type). We broke away from C in the handling of enums, why not arrays too?

— 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170126/e6246b5e/attachment.html>


More information about the swift-evolution mailing list