In chapter 10, I introduced parametric types to help explain how the union type works. We have also discussed parametric types in relation to collections such as arrays, dictionaries, and sets. With type parameters, you can restrict which elements can be used in your collections and thus get better type safety. However, all previous usage of parametric types have been as users of parametric types defined by others.
In this chapter, I will show you how to make your own parametric types and cover some common misconceptions and pitfalls when dealing with parametric types. But why would you want to make your own parametric types? Parametric types in Julia have two key benefits, which we will explore in detail in this chapter:
- Enabling more type-safe code. Julia can catch errors early at runtime, such as trying to put a string into an array of numbers.
- Improving performance when working with large datasets.
You will explore these topics through geometric code in 2D space (for simplicity). I will attempt to motivate you with a possible use in your rocket example project: to express the position of a rocket in space you need a Point type to represent a position in space and a Vec2D type to represent force, acceleration, and velocity in different directions.