Skip to content

Minimal C++ library for monad error handling with boost/std::optional

License

Notifications You must be signed in to change notification settings

octol/optional-monad

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

optional-monad

A minimal C++ library providing a few functions to be able to work with boost::optional as a monad. The idea is to add std::optional support in the very near future.

fmap

The fmap function applies the function object to the wrapped value, from which it creates and returns a new optional. Very loosely speaking, this makes the pair (optional<T>, fmap) a functor.

fmap :: optional T  -> (T -> S) -> optional S
auto fmap(const optional<T>& opt, Func&& func) -> optional<decltype(func(*opt))>;

bind

The bind function applies the function object to the wrapped value, unwraps one layer of optional<T> and returns a new optional. The pair (optional<T>, bind) is, again very loosely speaking, a monad.

bind :: optional T -> (T -> optional S) -> optional S
auto bind(const optional<T>& opt, Func&& func) -> decltype(func(*opt));

with

The with function is sort of a special case for when you want to mutate the wrapped value in-place.

Variant 1

The first overload takes the optional<T> by reference and mutates it.

with :: optional T& -> (T -> void) -> void
void with(optional<T>& opt, const Func& func);

Variant 2

The second overload takes the optional<T> by rvalue reference and moves it to the return value.

with ::: optional T&& -> (T -> void) -> optional T
auto with(boost::optional<T>&& opt, Func&& func) -> boost::optional<T>;

TODO

  • Wrap in a suitable namespace (what name? optm, optmonad, omonad, omon, opt_monad, optional_monad?)
  • Convert test to actual unit tests (let's try Catch).
  • Hook up unit tests to CI system (Travis-CI?)
  • Proper cross-platform build system (probably CMake).
  • Currently hardcoded for boost::optional. Make it work with std::optional out-of-the-box.
  • Add examples to README.
  • SFINAE checks for the function objects. This is needed for operator<< implementation.

Jon Haggblad [email protected]

Last update: 06 March 2017

About

Minimal C++ library for monad error handling with boost/std::optional

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published