ELI5: Explain Like I'm 5

Sum type

Imagine you have a toy box with different types of toys inside it. Each toy is of a different type, like a ball, a doll, or a truck. You can only play with one toy at a time, but you can choose which one you want to play with.

A sum type is sort of like a toy box for your computer program. Instead of toys, it holds different types of values, like numbers, strings, or booleans. Just like in the toy box, you can only use one value at a time, but you can choose which one you want to use.

The difference is that in a sum type, each value has a specific name or label, like "number", "string", or "boolean". This makes it easier for the computer program to keep track of what type of value it's using, and to make sure you don't use the wrong type of value by accident.

So just like you can pick a toy to play with from your toy box, your computer program can pick a value to use from a sum type. And just like you know what type of toy you're picking out, the program knows what type of value it's using thanks to the labels on the sum type.
Related topics others have asked about: