Daily Note - 2025-03-07
Hey, I'm Hanno! These are my daily notes on Crosscut, the programming language I'm creating. If you have any questions, comments, or feedback, please get in touch!
I previously showed you what records could look like. Let's continue today with another important building block, variants:
variant { Integer, Text }
A variant is a type that can be either of multiple values (for now, I'm calling
these "cases"). In this example, it could be either a value of type Integer
or
Text
. This is often called a "union" (which seems overly general, like it
could just as well apply to any composite type) or an "enum" (which seems overly
specific), or in programming language theory terms, a sum type.
Like records, variants are structurally typed and anonymous (by default; more on that later). They are also always tagged, which means that what the case of a specific value is, is stored as part of that value. If you encounter a value of the variant type from above, you could figure out what it contains at runtime, via pattern matching.