So far in this book, you’ve learned how to use polymorphism to create data structures and functions that can work with many different types. In all of the examples you’ve worked through so far, you’ve had to select a single type for any given instance of a polymorphic type. For example, while you can create a list that can hold any type, once instantiated, that list is restricted to only holding values of that one specific type that you selected.
In some cases, having data structures that are restricted to being uniform in the type they can hold can be quite inconvenient. In these situations, we turn to heterogenous data structures, which can hold values from an arbitrary collection of types. One particularly common example of a problem space where heterogenous data is helpful is in building tools to serialize and deserialize data. Throughout this chapter, you’ll work through building a tool to archive and extract sets of files, similar to the standard Unix tar command. As you’re working through this tool, you’ll get a chance to learn about three common approaches to dealing with heterogenous data. First, you’ll learn how to serialize and deserialize data using tools you are already familiar with, like sum types and records. Second, you’ll learn about a more powerful technique for building a serializer using existential quantification that will allow you to build a library that can support serializing any arbitrary collection of types. Third, in the next chapter, you’ll continue with this project by building tools to deserialize the archives that you’ve been creating.