Skip to content

Commit c29a9b9

Browse files
author
Guido van Rossum
committed
Describe type erasure when instantiating a generic class.
This addresses issue #79, but does not fix it (typing.py needs to be updated to implement type erasure).
1 parent 4e248ff commit c29a9b9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pep-0484.txt

+20
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,26 @@ is not generic but implicitly inherits from ``Iterable[Any]``:
407407
...
408408

409409

410+
Instantiating generic classes and type erasure
411+
----------------------------------------------
412+
413+
Generic types like ``List`` or ``Sequence`` cannot be instantiated.
414+
However, user-defined classes derived from them can be instantiated.
415+
Given a generic class ``Node[T]`` there are three forms of
416+
instantiation:
417+
418+
* ``x = Node()`` -- the type of x is ``Node[Any]``.
419+
420+
* ``x = Node[T]()`` -- the type of x is ``Node[T]``.
421+
422+
* ``x = Node[int]()`` -- the type of x is ``Node[int]``.
423+
424+
At runtime the type is not preserved, and the observable type of x is
425+
just ``Node``. This is type erasure and common practice in languages
426+
with generics (e.g. Java, Typescript).
427+
428+
429+
410430
Arbitrary generic types as base classes
411431
---------------------------------------
412432

0 commit comments

Comments
 (0)