Boost logo

Boost :

Subject: Re: [boost] Ternary logic programming (was: Re: [variant] Maintainer)
From: Joseph Van Riper (fleeb.fantastique_at_[hidden])
Date: 2015-07-02 05:36:55


On Wed, Jul 1, 2015 at 11:07 PM charleyb123 . <charleyb123_at_[hidden]> wrote:

> On Wed, Jul 1, 2015 at 6:49 PM, Niall Douglas
>
> > On 1 Jul 2015 at 15:53, charleyb123 . wrote:
> >
> > > Under an alternative model, the common construct would be something
> like:
> > >
> > > void foo(...) {
> > > return switch_(...) {
> > > ...
> > > }
> > > }
> > >
> > Out of interest, what do you think of my free function ternary logic
> > programming:
> >
> > tribool t;
> > if(true_(t)) { /* true */ }
> > else if(false_(t)) { /* false */ }
> > else if(unknown(t)) { /* other state */ }
> >
> So, after having looked hard for alternatives, I actually like your syntax
> (because I have a hard time finding a more obvious way to do that with
> if/else).
>
> IMHO, this is the key problem: if/else is just not very composable. Like
> the classic book, "The Art of Software Testing" discusses, refactoring
> if/else is amazingly tough (and branch analysis is a combinatorial
> explosion). It's hard to express and reason about, and refactoring usually
> means a rewrite.
>
> We can set up an "if/else-ladder" to otherwise do what a switch would do,
> but in the end, switch ends up being more clear-and-expressive. Easier to
> support and extend.
>
>
(Sorry so many snips, but I wanted to focus on something specific).

It probably isn't elegant, but maybe we could explore something like:

tribool t;

boost::if_( t,
   [&]( /*true condition's code*/),
   [&](/*false conditions code*/),
   [&](/* indeterminate conditions code*/)
);

Although there has to be a better way to clean it up... maybe with tagging
the arguments or something.

- Trey


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