Boost logo

Boost :

Subject: Re: [boost] Interest in an 'either' library?
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2013-06-24 11:40:06


On Mon, Jun 24, 2013 at 8:49 AM, Larry Evans <cppljevans_at_[hidden]>wrote:

> Could you explain exactly how Expected is better?
>

    either<ipv4, ipv6> get_ipaddr(); // X
    either<ipv6, addr_result> get_ipaddr(); // Y
    expected<ipv6, addr_result> get_ipaddr(); // Z

'either' _means_ the two result are equally valid. Or maybe doesn't imply
any relationship. 'expected' _means_ one value is expected (success) the
other is failure.

So the _meaning_ - the semantics - are different. 'expected' has a more
specific meaning, and should be chosen when that specific meaning is what
you are trying to convey. So Z is better than Y above - the intent is more
clear, more specific.

So *one* of the uses of 'either' is better handled with 'expected'.

That leaves the other use. how is 'either' better than 'variant':

    variant<ipv4, ipv6> get_ipaddr();
    either<ipv4, ipv6> get_ipaddr();

I suspect 'either' has benefits here, but is it enough?

And of course, what happens when someone adds another option:

    variant<ipv4, ipv6, ipv8> get_ipaddr();

(of course what will really happen is:

    variant<ipv4, ipv6, error> get_ipaddr();

instead of

    expected<either<ipv4, ipv6>, error> get_ipaddr();

and we've lost some meaning. Doesn't mean we shouldn't try!)

Also, why is 'either' only 2 types? In English, it isn't: "I am either
going on Monday, Tuesday, or Wednesday".

How common is 2 choices over 3 choices (outside of value + error)?

Tony


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