DEV Community

Discussion on: Explain Generics Like I'm Five

Collapse
 
nektro profile image
Meghan (she/her) • Edited

In strictly typed languages, Generics are a way to make an abstraction like a List or Map where the type isn't directly tied to the implementation. A Set for example, has standard operations that don't change whether you have a Set of cars or DOM Elements.


So you might set it up something like this.

class Set<E> {
}
public E get(int index) {
    return this._innerArray[index];
}
Set<Car> mySet = new Set<>();
Collapse
 
briankephart profile image
Brian Kephart

That explains why I wouldn't be familiar with the concept from Ruby or Javascript, since those languages are dynamically typed to begin with. Thanks!

Collapse
 
nektro profile image
Meghan (she/her)

Yeah np, glad I could help :D