Timeline for How should I store "unknown" and "missing" values in a variable, while still retaining the difference between "unknown" and "missing"?
Current License: CC BY-SA 4.0
17 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 2, 2018 at 12:14 | comment | added | exhuma | I've used the input of the current answers as guideline for a bit more research and came up with the same conclusion that discriminated unions are the cleanest way to solve this. Thanks for the detailed answer and patience :) | |
| Sep 2, 2018 at 12:12 | vote | accept | exhuma | ||
| Aug 27, 2018 at 17:22 | audit | First posts | |||
| Aug 27, 2018 at 17:23 | |||||
| Aug 16, 2018 at 16:56 | comment | added | Konrad Rudolph | @Luaan Right, ideally they can and do have consistent behaviour, namely to be monadic closures over a behaviour (see Eric Lippert’s answer). As to whether subclasses are overkill — this entirely depends on the verbosity of the language. In most mainstream languages you’re right though. | |
| Aug 16, 2018 at 16:47 | comment | added | Luaan |
@KonradRudolph Even that's overkill, really - just have a single immutable class that has three different static factory methods that each return one consistent possible object. Subclasses are only a good solution if you can have consistent behaviour with the superclass - if you'll need to do if (x is SomeUnknown) anyway, you're not really using polymorphism (sadly, I've met plenty of people who don't understand the difference, but still think they're doing "OOP").
|
|
| Aug 15, 2018 at 9:08 | comment | added | Konrad Rudolph | @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). | |
| Aug 15, 2018 at 8:25 | comment | added | Guran | +1 This is a typical case of "choose the proper tool for the job". | |
| Aug 15, 2018 at 7:43 | comment | added | David Arno | @KonradRudolph, very good point regarding eg C++ and other non "functional" languages that support DUs. I've slightly modified the wording to reflect that. | |
| Aug 15, 2018 at 7:41 | history | edited | David Arno | CC BY-SA 4.0 |
added 45 characters in body
|
| Aug 14, 2018 at 22:28 | comment | added | Frank Hileman | This is correct in general. For specifically numeric applications, NaN is typically used. And not integers. | |
| Aug 14, 2018 at 16:31 | comment | added | Konrad Rudolph | @Ewan No, it’s saying “Measurement is a datatype that is either … or …”. | |
| Aug 14, 2018 at 16:30 | comment | added | Konrad Rudolph |
@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).
|
|
| Aug 14, 2018 at 16:28 | comment | added | jk. | @KonradRudolph i'm not sure C++ unions count as discriminated unions - or did you mean something else? | |
| Aug 14, 2018 at 16:10 | comment | added | Ewan | isnt this saying Measurement is one of three possible datatypes rather than supplying a datatype? | |
| Aug 14, 2018 at 15:28 | comment | added | Konrad Rudolph | “[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. | |
| Aug 14, 2018 at 13:47 | comment | added | jk. | 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/… | |
| Aug 14, 2018 at 12:59 | history | answered | David Arno | CC BY-SA 4.0 |