Union/Sum Type

Let’s take an example of operating system information as a data structure to better understand sum types. Struct Our bare minimum struct to represent an operating system contains a few common fields that would exist in each of the operating system. typedef struct { char organization[]; int releaseYear; float version; int is_opensource; } os; It’s missing one thing though the type of operating system like macOS, linux, windows, etc. Enum We will add in a new field into our structure to represent the type of operating system by using an enum....

January 20, 2024 · 7 min · 1301 words · Lakshya Singh