Re:Ask HN: Good C++ code bases to read?
by jcelerier on Thursday October 29, 2020 @05:26AM
(#24928630)
> So, you get the best of both worlds.
I would really not call code that looks like this "best of both worlds"
KJ_IF_MAYBE(value, maybeValue) {
use(value);
} else {
handleNull();
}
when compared to if(!value)
handleNull();
use(*value);
With `kj::Maybe` and `KJ_IF_MAYBE`, using the syntax I demonstrated above, you check for nullness once and as a result of the check you get a direct pointer to the underlying value (if it is non-null), which you then use for subsequent access, so you don't end up repeating the check. So, you get the best of both worlds.
> it's likely ~30 loc to wrap std::optional
It's even easier to replace std::optional rather than wrap it. The value of std::optional is that it's a standard that one would hope would be used across libraries. But because it's flawed, people like me end up writing their own instead.