"Patterned" variadic generics
PEP 646 brought us variadic generics, which are great, but consider these functions: def product(it1: Iterable[T1], it2: Iterable[T2]) -> Iterable[Tuple[T1, T2]]: ... def zip(it1: Iterable[T1], it2: Iterable[T2]) -> Iterable[Tuple[T1, T2]]: ... def map(f: Callable[[T1, T2], S], it1: Iterable[T1], it2: Iterable[T2]) -> Iterable[S]: ... def make_boxed(x1: T1, x2: T2) -> Tuple[Boxed[T1], Boxed[T2]]: ... If I wanted to generalize them to arbitrary arguments, my naive solution would be something like this: def product(*it: *Iterable[Ts]) -> Iterable[Tuple[*Ts]]: ... def zip(*it: *Iterable[Ts]) -> Iterable[Tuple[*Ts]]: ... def map(f: Callable[[*Ts], S], *it: *Iterable[Ts]) -> Iterable[S]: ... def make_boxed(*x: *Ts) -> Tuple[*Boxed[Ts]]: ... But this obviously doesn't work. And it's also hard to see how it could be made to work because `Iterable` can't be unpacked. Has anyone thought about this? It seems like it would be a very useful feature for typing the standard library. (The title calls this "patterned" because that's what was used in this Swift design doc: https://github.com/hborla/swift-evolution/blob/variadic-generics-vision/visi... ) Regards, Thomas
What about a `builtins.map` analogue; `TypeMap[MyGeneric, *Ts]`: def product(it: Iterable[*Ts]) -> Iterable[TypeMap[tuple, *Ts]]: ... def zip(it: Iterable[*Ts]) -> Iterable[TypeMap[tuple, *Ts]]: ... def map(f: Callable[P, S], it: TypeMap[Iterable, P]) -> Iterable[S]: ... def make_boxed(x: *Ts) -> tuple[*TypeMap[Boxed, *Ts]]: ...
[Thomas] Has anyone thought about this? It seems like it would be a very useful feature for typing the standard library. [Joren] What about a `builtins.map` analogue
Yes, we had a `Map` operator in the variadic generics PEP but split it out because the PEP was getting too complex. Here's the draft [1] with some details. With PEP 646 taking a long time to land, we didn't spend more time on this. Given that there have been quite a few questions about this, we could revive this PEP after PyCon. (I won't have the bandwidth to do it before then.) [1]: https://docs.google.com/document/d/1szTVcFyLznoDT7phtT-6Fpvp27XaBw9DmbTLHrB6... -- S Pradeep Kumar
participants (3)
-
Joren Hammudoglu
-
S Pradeep Kumar
-
Thomas Kehrenberg