2 ms·
You would absolutely use them more, provided that the mechanism for working with them (pattern matching) is also in place. In F# coding you not only model your
by phillipcarter 6y ago
You would absolutely use them more, provided that the mechanism for working with them (pattern matching) is also in place. In F# coding you not only model your domain with sum types, but you operate on instances of them with pattern matching that gives you completeness checking. This does two things:
1. Allows you to make failure scenarios intrinsic to your domain, and the compiler then is a tool for domain correctness.
2. It provides a beautiful symmetry between how you define your domain and how you operate on it. The syntax is even almost identical. The features "fit" incredibly well because of that.
Beyond that, with optionals and results defined as sum types you find that any time you're in a "It may explicitly give me nothing" or "It may explicitly give me an error" scenario these types are the first your reach for in your toolbag.
Sum types don't completely obviate classes, but they are a better tool than classes for several scenarios.