Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do statically-typed languages deal without generics?

I'm curious which statically-typed languages have no generics support (and to a lesser extent which languages historically did not have generics), and how they deal with it.

Do users just cast all over the place? Is there some special sauce for basic collections, like lists and dictionaries, that allow those types to be generic?

Why do these languages not have generics? Is it to avoid potential complexity or other reasons?

like image 995
Roman A. Taycher Avatar asked Dec 16 '25 22:12

Roman A. Taycher


1 Answers

C—and historical C++, before it was called C++—requires you to either manually expand "generic" types into non-generics (i.e. the C preprocessor macro equivalent of C++ templates) or escape the type system (i.e. void pointers).

However, arrays (lists) are treated as composite types rather than a single type. You can have an array of shorts, for example, but you could not treat it the same as an array of chars or even of longs.

This isn't a really big problem in C, though inconvenient at times. It does represent a trade-off from 40 years ago, to put it in context.