ELI5: Explain Like I'm 5

Parametric polymorphism

Okay kiddo, let's start with talking about what a function is. A function is like a machine that takes in some stuff, does something with it, and gives out some stuff as a result. But sometimes we want to make a function that can work with different kinds of stuff. That's where parametric polymorphism comes in!

Parametric polymorphism is when we write a function that can work with different types of stuff, without us having to write a separate function for each type of stuff. Think of it like a Lego block that can fit onto different kinds of Lego structures.

So let's say we have a function that adds two numbers together:

```
function add(a, b) {
return a + b;
}
```

But what if we want to use this function to add two strings together? We can't do that with this function, because it only works with numbers. But with parametric polymorphism, we can make the function work with different types of stuff.

We can rewrite the function like this:

```
function add(a: T, b: T): T {
return a + b;
}
```

Notice how we added a `T` in there? That `T` is like a placeholder for any type of stuff that we want to use with this function. So when we call the function with numbers, it knows to treat `T` as a number. And when we call the function with strings, it knows to treat `T` as a string.

This allows us to write more flexible and reusable code, without having to write a separate function for each different type of stuff that we want to use with it. Pretty cool, huh?
Related topics others have asked about: