0
$\begingroup$

How programming languages are capable of handling generics ?

Here is some context on why I got the question.

Constructs like Lists, Dictionaries and Sets in Python can handle any type of data without even specifying the type of data being added. Whereas in Java Collections require you to specify the type at the time of creating those collections objects.

Both Python and Java are originally implemented in C\C++. So the capability of Java and Python to handle Generics should had obtained from C\C++. Even Javascript engines are built on C++.

To answer my own questions I started thinking why we need to specify the type when we are creating an object, my understanding for this is because to inform the compiler the amount of memory to be allocated in order for the object to live in Memory (RAM). Thus if we are creating an ArrayList in Java of say a MyRandomClass, it should likely allocate an array of Memory Blocks(MB) where each MB is of the size of MyRandomClass

However in Python a list can hold any type of Class without even specifying the type at the time of creation.

What happens under the hood ?

$\endgroup$
3
  • $\begingroup$ en.wikipedia.org/wiki/Type_erasure $\endgroup$
    – Pseudonym
    Jul 15 at 10:48
  • $\begingroup$ You seem to be confusing Programming Languages and Programming Language Implementations. Which of the two are you talking about? Also, there seem to be some misconceptions, e.g. you write "Even Javascript engines are built on C++", which is not true: Rhino, Nashorn, and dyn.js a written in Java, IronJS is written in F#, the reference implementation for ECMAScript 4 is written in Standard ML, BESEN is written in Pascal, and Narcissus is even written in JavaScript. $\endgroup$ Jul 18 at 7:38
  • $\begingroup$ @JörgWMittag I gave examples of where generics are already in practice to put some perspective and how generics are used in Python, Java and Javascript which have implementations in C++. So if it can be understood how they are done in their underlying C/C++ that should give an understanding on how generics can be implemented in general. The idea behind mentioning programming languages in my question is how underlying implementations can handle variations for ex:- List in Python and ArrayList in java. $\endgroup$ Jul 19 at 9:34
0
$\begingroup$

As an example: Apple's Swift compiler will compile the required code for every type needed, then compare the various versions, and if two are identical then one of them is picked.

For example, lots of the code for an array of unsigned 32 bit integers and for an array of signed 32 bit integers may very well be identical, while the code for an array of 64 bit integers, signed or unsigned, would be different. The code for an array of any reference type might be different.

It's up to the language designer and to some extent the designer of the standard library to make sure not too much unnecessary code is being produced.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.