| > When are you creating functions take or return radically different types that need to be expressed this way Let's say you're opening a file that you think is a CSV. There can be several outcomes: - the file doesn't exist - the file can't be read - the file can be read but isn't a valid CSV - the file can be read and is valid, and you get some data All of these are different types of results. You can get away with treating the first 3 as the same, but not the last. Without a tagged union, you'll probably resort to one of a few tricks: - You'll have some sort of type with an error code, and a nullable data field. In reality, this is a tagged union, it's just that your compiler doesn't know about it and can't catch your errors. - you'll return an error value and have some sort of "out" value with the data: this is basically the same as the previous example. - you'll throw exceptions, which usually ends up with people writing code that forgets about the exception because the compiler doesn't care about it, and the code works 99% of the time until it completely blows up. |