123: Data Types: Tuples.
Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorías:
Sometimes you need to bundle things together so you can treat them as a single unit. That’s what the tuple provides. You might wonder why not just use a container to put things in? Containers are good at storing as many instances of some type as you want. But everything that goes into a container has to be the same type. Sure, you can put pointers in a container and then have different specific derived types in the container. But the container still only holds pointers of a specific type. A tuple allows you to put completely unrelated types inside. It does this by defining the number and type of each item the tuple can hold. This means you can’t just put whatever you want into any tuple. But you can declared a custom tuple to hold whatever you want. Sometimes you only need to store two things. This is usually called a pair and has been around for quite a while. It allows you to store both a key and a value, for example, as needed by a dictionary. You shouldn’t use a pair or a tuple to hold instances of types like a general purpose collection. If we take the backpack example from an adventure game, then you wouldn’t want to use a pair to describe the backpack contents. That would mean that the backpack could only hold two items. But you also wouldn’t want to use a tuple either even though a tuple can hold as many items as you want. A tuple can hold as many things and of whatever type you want but this has to be defined at compile time. For a backpack, the best solution is to keep using a collection class and store pointers or interfaces to some common aspect that all items will share. This allows you to treat each item in the backpack the same and then the hero can store however many items needed at runtime. Anytime you’re creating code that needs to work with a group of unrelated types and you don’t know ahead of time what those types are or what they mean, then that’s when you can use a tuple. Listen for more on tuples or you can also read the full transcript below. Transcript You might wonder why not just use a container to put things in? Containers are good at storing as many instances of some type as you want. But everything that goes into a container has to be the same type. Sure, you can put pointers in a container and then have different specific derived types in the container. But the container still only holds pointers of a specific type. A tuple allows you to put completely unrelated types inside. It does this by defining the number and type of each item the tuple can hold. This means you can’t just put whatever you want into any tuple. But you can declared a custom tuple to hold whatever you want. You might hear some people refer to a tuple as a toople. I call them tuples. Sometimes you only need to store two things. This is usually called a pair and has been around for quite a while. It allows you to store both a key and a value, for example, as needed by a dictionary. Check out episode 44 for more information about dictionaries. Let me first say that you shouldn’t use a pair or a tuple to hold instances of types like a general purpose collection. If we take the backpack example from an adventure game, then you wouldn’t want to use a pair to describe the backpack contents. That would mean that the backpack could only hold two items. But you also wouldn’t want to use a tuple either even though a tuple can hold as many items as you want. A tuple can hold as many things and of whatever type you want but this has to be defined at compile time. For a backpack, the best solution is to keep using a collection class and store pointers or interfaces to some common aspect that all items will share. This allows you to treat each item in the backpack the same and then the hero can store however many items needed at runtime. You use a pair or a tuple when you have a specific set of types that you want to group together. I’ll continue the explanation