Explain Generics Like I'm Five

I often see discussion about generics, but I've never really grasped what they are. I'm a Ruby/Rails developer, and the term doesn't seem to be used in that community.

Did you find this post useful? Show some love!

Everybody likes stories and adventures. You yourself like them so much, that your father drew a story book specifically for you, where the main character goes on a journey and encounters lots of new and interesting things.

Now, you like unicorns, so the book your father drew for you has a unicorn as the main character. Your sister also really likes the story, but she prefers dragons to unicorns, so your father recreates the entire book, but with a dragon as the main character. Your best friend also really likes the story, but he'd like labrador as the main character, so your father obliges and draws yet another version of the story. Sounds like a lot of work, right?

Well 'generics' is as if your father made single book, but left a hole on every page, in such a way that you can place a picture of your main character of choice on the back of the book (exactly where all the holes line up), and suddenly the whole story is about your main character. From now on, all you need to do is make a main character for yourself, and you can have your very own version of the story, with no effort whatsoever.

I think this is the best explanation, but you might want to add how this maps to code as this is very abstracted away from code.

Given that this is an #explainlikeimfive post, abstracting away from the code is rather a fundamental requirement :).

But for completeness sake: the original books would have been the UnicornBook, DragonBook and LabradorBook classes, which are nearly identical expect wherever the Unicorn, Dragon or Labrador classes (or their instances) are used. The generic equivalent would ba a Book where T can be added for whatever you like, to create e.g. Book< Unicorn > or Book< Eagle >.

Hope that helps :-).

Heh, totally get where your coming from.

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<>();

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!

Here is a nice explanation from Scala, using the stack as an example.


Classic DEV Post from Mar 23

I'm developing software and I don't Google any more.

How to improved your search results when developing software

Follow @jjaimelr to see more of their posts in your feed.
dev.to is now open source!
View Announcement Post View GitHub Repo
Brian Kephart
I play guitar and bass. Sometimes I code.
Trending on dev.to
How does the web look if everyone owned their own data?
What's your origin story?
#career #learn #webdev
Explain Hashing + salting Like I'm Five
#explainlikeimfive #webdev
Being average in > 1 things is easier and more valuable than being excellent at 1 thing.
#career #learning
Do we need standup?
#agile #discuss #productivity
#discuss What language do you think in?
#discuss #languages #programming
What are your thoughts on multiples package managers?
What are common myths about software careers?
#discuss #career