|
|
|
|
|
by Timon3
946 days ago
|
|
Your tuple example only works if both tuples have two elements. I specifically mentioned arbitrary fixed-size tuples (as in, tuples with an arbitrary non-variable length). Your generic type transformation example also doesn't come close to what Typescript does. The resulting dict will not have known keys based on the keys of the input dict. In Typescript I can write a function that takes an object with known keys, and it returns an object with those same keys having their values mapped to a different type, with the keys still known. I threw together a quick example - just look at the type of resultA/resultB by hovering over the variables[0]. But both are great examples - they are probably the closest you can get in Python, and they are so far removed from the thing I want to represent that they are completely useless. [0]: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAhjAvDA... |
|
The second example is cool. But I can't find a good practical use case for either example.
If you have a collection that's both heterogeneous and whose [size/key set] is statically known, when would it make sense to apply such generic transformations to them? This sounds like you have tuples or dataclasses where all elements have different meanings (since their types are fixed and different) _and_ you want to treat them like a generic collection _and_ you need the type checker to infer the result type.
The main use of tuples or dataclasses or `NamedTuples` is to pass or return values to/from functions without dealing with long lists of arguments. The elements aren't in the same category, it doesn't make sense to process them as one big collection, they mean different things.
(Also I think you made a mistake in your previous message, you wrote "an object with each key turned into a function" but it's the values that change types here.)