Expressiveness, Nullable Types, and Composition

Rafael Varago
1 min readNov 14, 2019

--

An introduction to the C++ library absent.

I have created a small C++ library called absent: https://github.com/rvarago/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.

It offers some useful combinators, for instance, bind, fmap, eval, alongside an infix notation based on operator overloading, that aim to reduce boilerplate from our daily C++ code while increasing its type-safety and expressiveness.

One of its main purposes is to refactor this kind of code snippet:

auto const maybe_person = find_person();
if (!maybe_person) return;

auto const maybe_address =1 find_address(*maybe_person);
if (!maybe_address) return;

auto const zip_code = zip_code(*maybe_address);

To a declarative pipeline of composed functions that pushes the checking against emptiness to the very end of the chain, avoiding repetitive error handling, and therefore reducing the chances of accidental invalid accesses, like this:

auto const maybe_zip_code = find_person()
>> find_address | zip_code;
if (!maybe_zip_code) return

The following links lead to the series of two guest posts that I wrote for Jonathan Boccara’s Fluent C++ that describes the motivation behind absent, related projects, and how to make use of it.

--

--

Rafael Varago
Rafael Varago

Written by Rafael Varago

Software Engineer interested in C++, Rust, Haskell, Scala, Go, C, Python , Linux, functional programming, system programming, tooling, IoT, cloud, math, etc.

No responses yet