Roman A. Taycher
Roman A. Taycher

Reputation: 19477

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?

Upvotes: 3

Views: 1114

Answers (5)

Hans Kesting
Hans Kesting

Reputation: 39255

C# didn't support generics until v2.0. So yes, then you needed a lot of casting from Object.

I guess the same goes for VB.Net.

Upvotes: 0

Michael Ekstrand
Michael Ekstrand

Reputation: 29090

Pascal, in its original forms, did not support generics. If you wanted a linked list, you needed to make one for your specific type (e.g. IntLinkedList).

Modern versions of Pascal (e.g. ObjectPascal/Delphi) may provide some form of generics.

Upvotes: 0

user207421
user207421

Reputation: 310840

The short answer to this is C++ templates. Unlike generics, which restrict existing types, templates are a way of generating new types at compile time. Like most code-generating solutions, it's not a very satisfactory one: hence we have moved on.

Upvotes: 0

Roger Pate
Roger Pate

Reputation:

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.

Upvotes: 2

nanda
nanda

Reputation: 24788

Download java 1.4 or 1.3 and try it yourself.

Hint: Yes there will be probably many casts

How to deal: I've seen an organization forcing any API not to use collection (in the method declaration) but array to avoid confusion to the user. Alternative is to create a specific collection classes that only works with certain class for example StringList etc

Upvotes: 1

Related Questions