absent is a tiny open-source C++ library meant to simplify the composition of nullable types in a generic, type-safe, and declarative style.


I have started a tiny open-source C++ library called absent inspired by functional programming languages, e.g. Haskell and Scala, whose purpose is to simplify the functional composition of nullable types, such as, but not limited to, [std::optional](https://en.cppreference.com/w/cpp/utility/optional).

absent offers some useful combinators to make the composition of nullable types more expressive, e.g. and_then, transform, eval. Furthermore, it also supports infix notations based on operator overloading that aim to reduce boilerplate when chaining operations on nullable-types while increasing type-safety and expressiveness.

As an example, consider the following snippet:

auto const person_opt = find_person();  
if (!person_opt) return;  
  
auto const address_opt = find_address(*person_opt);  
if (!address_opt) return;  
  
auto const zip_code = get_zip_code(*address_opt);

We can use absent to refactor this piece of code into a declarative pipeline of functions and push the checks against emptiness to the very end of the chain, avoiding repetitive and entangled error handling:

auto const zip_code_opt = find_person()  
                            >> find_address
                            | get_zip_code;  
if (!zip_code_opt) return

I briefly mentioned absent before in the context of total functions.

Additionally, the following links lead to the series of two guest posts that I wrote at Jonathan Boccara’s Fluent C++, where I described the motivation behind absent and how we may profit from it:


Originally published at https://medium.com/@rvarago