Boost logo

Boost :

Subject: Re: [boost] [optional] Safe optional
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2014-11-27 06:51:54


Vicente J. Botet Escriba <vicente.botet <at> wanadoo.fr> writes:

>
> Le 17/11/14 18:14, Joaquin M Lopez Munoz a écrit :
> > Just to throw some idea in, there's an altenative based on the following
> > approach:[...] we could *lift* f so that we write this instead:
> >
> > auto ret=lift<f>(arg1,...,argn);
> >
> > with the same semantics. This is a case of *monadic lifting* as
> > explained (and implemented) at
> >
> > http://bannalia.blogspot.com/2014/03/monadic-lifting-in-c.html
>
> Hi Joaquin, I missed your post.
>
> I like this, of course :)
>
> If I understood your blog, this should be something like
>
> auto ret = lift<optional, f>(a1, ..., an);
>
> Or could the optional template be deduced?

It would be more like

 lift<optional>(f)(a1,...,an)

but in any case the particular monad being lifted to (here optional)
must be specified.
 
> doesn't scales to expected<T,E>. Instead of a template <class>
> parameter, lift should take a type constructor, In Boost.Expected we
> would do this as
>
> auto ret = lift<expected<_holder, error_code>, f>(a1, ..., an);
>
> where _holder is a holder type that makes expected<_holder, error_code>
> to behave as a type constructor.

Absolutely, additional template parameters like in expected would
call for a type constructor.

> This lift operation is a good thing when you have a function. The
> problem is that people don't have always a function to call, they have
> just code to execute. The closer we have are lambdas
>
> auto res = fmap(o1, ..., on , [](a1, ..., an) {
> // the code
> });

You can lift lambdas as well:

  auto res=lift<optional>([](int x,int y){
    return ...;
  })(o1,o2);

Joaquín M López Muñoz
Telefónica


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk