</>CodexBash
All articles
Programming

Generics

Generics explained with Java, TypeScript and Python examples: why type parameters beat Object and any, generic classes and methods, bounded types, wildcards and the PECS rule, type erasure in Java, generic constraints in TypeScript, type hints with TypeVar in Python, and common mistakes.

CodexBash, , 6 min read

A list of strings, a list of orders, a list of numbers: the logic of a list is the same whatever it holds. Generics let you write that logic once while keeping the compiler aware of exactly which type is inside, so names.get(0) is known to be a String without a cast and adding an Integer to it is a compile-time error. Generics are everywhere in modern code, in collections, Optional, Promise, Map, Result types and API clients, and understanding them makes both using and writing libraries much easier. This article explains them with examples in Java, TypeScript and Python.

Sign in to read the rest

The whole 6 minute article is free with an account, along with every problem, lesson and tutorial. No card.