[Python-ideas] Generics Syntax

אלעזר elazarg at gmail.com
Thu Sep 15 05:21:25 EDT 2016


This suggestion is so obvious that it's likely has been discussed, but I
can't find any reference (It's not what PEP-3124 talks about).

Generic class syntax, now:

    _T_co = TypeVar('_T', covariant=True)

    class Container(Generic[_T_co]):
        @abstractmethod
        def __contains__(self, x: _T_co) -> bool: ...

(yes it's object in reality)
Generic class syntax, suggested:

    class Container[+T]:
        @abstractmethod
        def __contains__(self, x: T) -> bool: ...

The + signifies covariant type, as in Scala. The need for underscore prefix
and explicit name for covariant types is gone, since it's class-scoped.
The + is a bit cryptic, but so are term "covariant" and Generic[_T_co]. The
syntax by large is exactly what users coming from statically-typed
languages will expect.

Giving a bound can be done using "T <: AnyStr", "T: AnyStr" or any other
syntax.

Again, I assume it was discussed before, but my Google skills are failing
me; a pointer will be helpful.

~Elazar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160915/d8d102f6/attachment.html>


More information about the Python-ideas mailing list