3 ms·
Another take on this is the "Kroll result" after Rachel Kroll of rachelbythebay fame. Basically, it's a C++ type "Result<T>" with methods like "bool isError()"
by red_admiral 4y ago
Another take on this is the "Kroll result" after Rachel Kroll of rachelbythebay fame. Basically, it's a C++ type "Result<T>" with methods like "bool isError()" and "T get()" or something like that, but the twist is that it contains a private boolean field "checked" initialized to false, but set as a side-effect of calling isError. If you call get() and checked == false, then it blows up with an exception (I presume it does that too if you call get() and it's actually an error).
That way, if you ever write code that doesn't error-check before trying to get the "happy path" result, you'll notice immediately, even if it's the kind of thing that very rarely fails and you haven't written a unit test for it.
A chaotic good CS educator might want to try out an experiment: give the students an assignment where they have to work with an API that uses a normal result type, explain why error-checking is really important, but when you're marking the assignment switch out the result type for the Kroll one.
- prng2021 4y agoInteresting! I never heard of this pattern but will consider using it. Any idea why it’s not more popular?
- tialaramex 4y agoI guess you mean this ? https://rachelbythebay.com/w/2022/02/21/result/ https://rachelbythebay.com/w/2022/02/21/result/ The type isn't described there as "Kroll result" perhaps out of modesty, but I also found no other reference to this type by that name, and it seems more like an unfinished thought than an actual type you'd use. Rachel seems fixated on people learning a lesson from using it wrong, rather than shifting hard left like Rust's Result so that when you get this wrong your code doesn't compile the same as if you forget to quote strings, or you write the actual arithmetic product symbol instead of an asterisk. There is no need for this code to compile in order to "learn" something. That's the same lesson which scraped into C++ 20 errata, to make std::format("{} {}", 5); into a compile error whereas previously it was ludicrously a runtime error. Maybe at runtime the literal 5 will be two values ? No. We don't need to execute this code to realise that it's wrong. Shift hard left.
- red_admiral 4y agoI do indeed. I think, reading between Rachel's lines, that she uses this in practice in her own projects? "Kroll result" is a term I just made up myself. I'm guessing Rachel's decision was not so much fixation and more that only the standards body (or the designer of a new programming language) could make something into a compile-time error, but a SWE/SRE can take a small step like this on her own.