Demystifying Union Types in Dart, Tagged vs. Untagged, Once and For All
Over the years and throughout my career, I have written a lot of code in various languages, such as PHP, Python, Javascript, Typescript, Dart, Java, Go, Clojure, and more. All these languages are equally good and have unique aspects that make you approach problem-solving in a certain way.
One concept seems to appear frequently, often in a rather basic manner but in fact quite complicated: Union Types. Some languages naturally support them; in others, it may seem like an impossible task.
In this article, we will be diving deep into Union Types (Untagged and Tagged Unions)—what they are, why they’re helpful, where they fall short, and why, in strongly typed languages like Dart, you might want to reconsider their use. To convey my message better, I will write examples from different languages, including Typescript, F#, PHP, and more.
Without further ado, let’s get started.