Matching and recursive generics types:

It would be possible to have generic types with pattern matching?

class A: ...
class O: ...

class S[T]:
    def __init__(self, prev: T) -> None:
        self.prev = prev

def add[G,T](a: G[O], b: T) -> G[T]: ...
    # some implementation here

test = add(S(O()), A())

And then the type of test could be inferred by mypy or any other tool as S[A] because the S(O) is pattern matched with the G[O].

Also, I would find interesting recursive inferences, within a function, but that may be even more complex.

Higher kinded types have come up in various discussions in the past. However, there’s very few type systems that actually implement them, due to their high implementation complexity compared to the relatively small gain in expressiveness.

I don’t think we will see higher kinded types in Python any time soon, if ever, there’s many more pressing issues that need to be solved first and if we’re talking about new features, that do provide a lot of value, the next big thing is probably type intersection, but even that is currently in question of whether or not it can be replaced with something more simple, that still provides mostly the same amount of value.

That being said, I would love to have higher kinded types, since they do enable cool things (not necessarily that useful, but certainly cool), I just have realistic expectation about them.


Many thanks for letting me know the name of it, didn’t know that “Higher kinded types” were a thing :slight_smile:
Now i found this page and this one, and it seems they can be emulated and that there is a mypy plugin for them, thought it would be very cool to have this sort of thing in the standard language.