Toward a Monads proposal - providing generic make_ and .then() functions

205 views
Skip to first unread message

Vicente J. Botet Escriba

unread,
Jan 26, 2014, 3:11:33 AM1/26/14
to std-pr...@isocpp.org
Hi,

We will have

template <class T, class... Args> unique_ptr<T> make_unique(Args&&...
args);
template<class T, class... Args> shared_ptr<T> make_shared(Args&&...
args);

and possibly

template <class T> constexpr optional<decay_t<T> make_optional(T&&);
template <class T> constexpr future<decay_t<T>> make_ready_future(T&&);
template <class T> constexpr expected<decay_t<T>> make_expected(T&&);


It would be great to have a generic make function taking the monad as
template parameter in addition

template <class M, class X>
constexpr M make(X&&);

template <template<class...> M, class X>
constexpr M<X> make(X&&);

that can be used as

int v=0;
make<shared_ptr>(v);
make<unique_ptr>(v);
make<optional>(v);
make<future>(v);
make<shared_future>(v);
make<expected>(v);

or as

make<shared_ptr<uint&>>(v);
make<unique_ptr<uint&>>(v);
make<optional<uint>>(v);
make<future<int&>>(v);
make<shared_future<int&>>(v);
make<expected<int>(v);

Note that currently it is not possible to do the following

future<int&> f = make_ready_future(v);

as the future value_type is decayed

and that the function make_ready_shared_future() has not been proposed
as the user can

auto f = make_ready_future(v).share();


IMO, these corner cases should be avoided and having a generic interface
would help to make generic algorithms.

The definition off these make function could be based on the Monad
library defined in "Monads in C++" (see
http://yapb-soc.blogspot.fr/2012/10/monads-in-c.html)

The linked implementation doesn't contains the variadic emplace version
of mreturn which shoul dbe also added to take care of the existing make_
functions.

template <class M, class ...Args>
constexpr M make(Args&&...);

Of course Monad provide also the generic function mbind (similar to the
future::then() proposal).
Making these functions generic would help to build on top of algorithms
working on monads.

Would you like to see something like this on the standard?

Best,
Vicente




Ivan Čukić

unread,
Jan 27, 2014, 2:19:15 PM1/27/14
to std-pr...@isocpp.org

Would you like to see something like this on the standard?


+1 for generality as opposed to introducing make_* for everything under the Sun.

It would also allow nice chaining of asynchronous processes even without the introduction of resumable functions.

Thanks for starting this.

---
Ivan Čukić - ivan (at) kde.org
KDE developer and Free/Libre software enthusiast

Reply all
Reply to author
Forward
0 new messages