Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

13
  • 7
    you can do sum types in OO languages but there is a fair bit of boiler plate to make them work stackoverflow.com/questions/3151702/… Commented Aug 14, 2018 at 13:47
  • 11
    “[in non-functional languages languages] this pattern isn't likely of much use to you” — It’s a pretty common pattern in OOP. GOF has a variation of this pattern, and languages such as C++ offer native constructs to encode it. Commented Aug 14, 2018 at 15:28
  • 14
    @jk. Yes, they don’t count (well I guess they do; they’re just very bad in this scenario due to lack of safety). I meant std::variant (and its spiritual predecessors). Commented Aug 14, 2018 at 16:30
  • 2
    @Ewan No, it’s saying “Measurement is a datatype that is either … or …”. Commented Aug 14, 2018 at 16:31
  • 2
    @DavidArno Well even without DUs there’s a “canonical” solution for this in OOP, which is to have a superclass of values with subclasses for valid and invalid values. But that’s probably going too far (and in practice it seems that most code bases eschew subclass polymorphism in favour of a flag for this, as shown in other answers). Commented Aug 15, 2018 at 9:08