220 38116 <bbbc65c3-b9e5-4365-9d06-591fcd86d93a@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: tortoise741@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Convert a variant into an optional?
Date: Thu, 17 May 2018 03:30:12 -0700 (PDT)
Lines: 301
Approved: news@gmane.org
Message-ID: <bbbc65c3-b9e5-4365-9d06-591fcd86d93a@isocpp.org>
References: <1c089cc9-4f8a-4778-b92b-2d3b95b211e1@isocpp.org>
 <2e6b1fb6-e7e1-fc87-bb42-b5179e826638@wanadoo.fr>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_815_1324439873.1526553012730"
X-Trace: blaine.gmane.org 1526552889 14204 195.159.176.226 (17 May 2018 10:28:09 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Thu, 17 May 2018 10:28:09 +0000 (UTC)
Cc: tortoise741@gmail.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDJ35HPXFAILLM7V24CRUBBYXGJKY@isocpp.org Thu May 17 12:28:05 2018
Return-path: <std-proposals+bncBDJ35HPXFAILLM7V24CRUBBYXGJKY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-vk0-f71.google.com ([209.85.213.71])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDJ35HPXFAILLM7V24CRUBBYXGJKY@isocpp.org>)
	id 1fJG8h-0003YK-Vq
	for gclcip-std-proposals@m.gmane.org; Thu, 17 May 2018 12:28:04 +0200
Original-Received: by mail-vk0-f71.google.com with SMTP id w84-v6sf3445087vkw.2
        for <gclcip-std-proposals@m.gmane.org>; Thu, 17 May 2018 03:30:15 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=fB0E5fIYISairj8hRnnXzVnumzdR1LGt5EwabW/XGjo=;
        b=S4iKViknQzXlN3ZS/vVWzL7M6O/W2ox9Wh/jKb+VLpmt+q609sjxq3Nhp6ysR9EYn5
         6rXuLNrbYWE2Im/gQb+PzOZdQXSyVivkgblBdlZs7xNb6ddDsvU2/X9reBK4/aZJ1sPw
         YOEGXTKsR5/6s7s0d0B4lHxNrZx+NFriaE7BKeLOSRbwOdcE3BM4zeOQfMrg560rzyqA
         Y/ZXHJpP7LdUcTz7g5cWXfU6Fl6v6FIe3W6WCOhq2uiDc5FdTblBTfCnwdDiJ8T4UYy/
         ByfW78mZJMPOLhp56s1xaTUqeeaRb76px//cUUMA/d90GTlU1g00Ylmk8QBBIzjwNOhM
         XV8A==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=fB0E5fIYISairj8hRnnXzVnumzdR1LGt5EwabW/XGjo=;
        b=XFZDlBG3CqAnRHlxkX/yiuYmbi7Swzx2Sq8CO82fkvou42LP9mpsQVGf6OkmowlTy0
         0svvL47397YFmRQ0A3QPPZp9MVO+i/IYFlZK4cpIQjQUI4wYLkIedtZBNhghTdZ8vPBn
         rcVOuTjWftPQstPjnb/vMl2jDuBOW7mcVmZyPHIizA5XJMujlhwSHuCu0oEOMp09Ho58
         2b3nFN1CzlWTpvmkOvmzT4FPvlronzwQHE+v5kfSJTs/F4KLQ+sNTC8var1Hna+N6ST3
         2f44CuU55G2jBkS8G0jd7hx4uMaHtXHWl1mgLHKm40of2QQmv3xOw6B2AirmaeAX7eHm
         oxUw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to
         :references:subject:mime-version:x-original-sender:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=fB0E5fIYISairj8hRnnXzVnumzdR1LGt5EwabW/XGjo=;
        b=gzUKkY5AgM3nZJJ6uG8lqYRYfpYrxxDgZwOlE0g6pRqMZ1w5jdXdwIx27XHwmV+s0T
         WhMFsphjhDq1r4qrtbqgmG4IuLTkLAi908a6qooUprGMT1CmVRlI5E+DMn07VwZc2OcC
         nx47e/2s63NHRHmk77JZiq1mafSG5E2WmaZ3hvK1uhEQU048BcQ8/2I7/eU4S8TwwPSK
         1Yuq4eCfKbHAGwNNNxsKOgpUNXuXbyvoD08MkBHeZRk2bBTBeLOPwcPYLN61/Nj++0Tk
         Q4lbz4spfzSX4dFXuc1hS09N5x1JKjRsNdwjW6MGdOqGSgnOKojS6gD1xkZ3zNy/vvZr
         SQzQ==
X-Gm-Message-State: ALKqPwfxsBZcdOibrAIpclXLUEbZCYWeQQc86fKKb4ndweUh75QjrFnx
	+VqOqVN49RuyrhUU/9CUM1Bcqw==
X-Google-Smtp-Source: AB8JxZrmVwHubeDgsYsUkvn3ZKhEDWfpGdwVRv2M2ahX+MID0Y1B9hdQQpuq7aKQRLqSA9WJWSStKg==
X-Received: by 2002:ab0:4c20:: with SMTP id l32-v6mr3095590uaf.45.1526553014336;
        Thu, 17 May 2018 03:30:14 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 2002:a1f:3f50:: with SMTP id m77-v6ls2981996vka.3.gmail; Thu, 17
 May 2018 03:30:13 -0700 (PDT)
X-Received: by 2002:a1f:951:: with SMTP id 78-v6mr638388vkj.0.1526553013274;
        Thu, 17 May 2018 03:30:13 -0700 (PDT)
In-Reply-To: <2e6b1fb6-e7e1-fc87-bb42-b5179e826638@wanadoo.fr>
X-Original-Sender: tortoise741@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: std-proposals@isocpp.org
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:38116
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/38116>

------=_Part_815_1324439873.1526553012730
Content-Type: multipart/alternative; 
	boundary="----=_Part_816_571158546.1526553012731"

------=_Part_816_571158546.1526553012731
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Tuesday, 15 May 2018 22:34:36 UTC+1, Vicente J. Botet Escriba wrote:
>
> Le 15/05/2018 =C3=A0 17:39, torto...@gmail.com <javascript:> a =C3=A9crit=
 :=20
> > Hi,=20
> >     Apologies if this has been asked before. variant and optional are=
=20
> > quite common search terms.=20
> >=20
> >     I'm wondering if there is, or should be a standard way to convert=
=20
> > a variant into an optional?=20
> > Or if its a bad idea why there shouldn't be.=20
> >=20
> > Consider the following example. Imagine Foo is an object representing=
=20
> > a configuration.=20
> > One part of the configuration bar has two possible modes of operation=
=20
> > one requires one piece of configuration=20
> > and one requires the other but it is not valid to specify both.=20
> >=20
> > #include <variant>=20
> > #include <optional>=20
> >=20
> > class Foo=20
> > {=20
> > private:=20
> >     std::variant<int, std::string> bar;=20
> > public:=20
> >     Foo(int a): bar(a) {}=20
> >     Foo(const std::string& b): bar(b) {}=20
> >=20
> >     std::optional<int> getInt() const=20
> >     {=20
> >         std::optional<int> ret;=20
> >         auto got =3D std::get_if<int>(&this->bar);=20
> >         if (got) ret =3D *got;=20
> >         return ret;=20
> >     }=20
> >=20
> >     std::optional<std::string> getString() const=20
> >     {=20
> >         std::optional<std::string> ret;=20
> >         auto got =3D std::get_if<std::string>(&this->bar);=20
> >         if (got) ret =3D *got;=20
> >         return ret;=20
> >     }=20
> > };=20
> >=20
> > There seems to be a lot of boilerplate to convert the variant into a=20
> > optional for the return.=20
> > There is an inefficiency here in that the type contained has to be=20
> > checked twice to use this.=20
> >=20
> > There are other ways to do this. You can expose the variant member and=
=20
> > use a visitor=20
> > but using a visitor feels like overkill for this. Also the variant is=
=20
> > an internal implementation detail.=20
> > An equally valid implementation would have two different optional=20
> > members (but would not enforce their mutual exclusivity).=20
> >=20
> > So do we need (or is there already) something kind of like:=20
> >=20
> >     std::optional<std::string> getString() const=20
> >     {=20
> >         return make_optional<std::string>(this->bar);=20
> >     }=20
> >=20
> > or bikeshedding:=20
> >=20
> >     std::optional<std::string> getString() const=20
> >     {=20
> >         return to_optional<std::string>(this->bar);=20
> >     }=20
> >=20
>
> The conversion is simple, but we don't need to do the conversion until=20
> requested. We can see a variant<Ts...> as some kind of ValueOrNone type=
=20
> (that can not change of alternative) once we select one of the=20
> alternatives T. Lets name this function select<T>(SumType).=20
>
> The conversion would be done only when requested, e.g. by an implicit=20
> conversion.=20
>
>      optional<T> o =3D select<T>(SumType);=20
>
>
> However, we could as well=20
>
>      auto von =3D select<T>(SumType);=20
>
> von is not an optional<T>. It is something else, a reference to a=20
> sum-type, with the perspective of only one of the alternatives. Now we=20
> can use von as a ValueOrNone, with the usual von.has_value() and *von.=20
> Note that these functions don't need to check the type twice:=20
>      von.has_value() should be equivalent to std::get_if<T>(&von) and=20
>      *von equivalent to std::get<T>(von).=20
>
>
> Just my 2cts.=20
>
> Vicente=20
>
> I very much like the name select.

I see your argument that a ValueOrNone isn't strictly an optional but if it=
=20
isn't don't we complicate the language
with yet another "maybe" type? Wouldn't the language be cleaner with just=
=20
one?
I guess the answer is adding optional references as previously mentioned?

Regards,

Bruce.

=20

--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/bbbc65c3-b9e5-4365-9d06-591fcd86d93a%40isocpp.or=
g.

------=_Part_816_571158546.1526553012731
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Tuesday, 15 May 2018 22:34:36 UTC+1, Vicente J. Botet E=
scriba  wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Le 15/05/2018 =
=C3=A0 17:39, <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mail=
to=3D"2h2c0_gOCQAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;javasc=
ript:&#39;;return true;" onclick=3D"this.href=3D&#39;javascript:&#39;;retur=
n true;">torto...@gmail.com</a> a =C3=A9crit=C2=A0:
<br>&gt; Hi,
<br>&gt; =C2=A0=C2=A0=C2=A0 Apologies if this has been asked before. varian=
t and optional are=20
<br>&gt; quite common search terms.
<br>&gt;
<br>&gt; =C2=A0=C2=A0=C2=A0 I&#39;m wondering if there is, or should be a s=
tandard way to convert=20
<br>&gt; a variant into an optional?
<br>&gt; Or if its a bad idea why there shouldn&#39;t be.
<br>&gt;
<br>&gt; Consider the following example. Imagine Foo is an object represent=
ing=20
<br>&gt; a configuration.
<br>&gt; One part of the configuration bar has two possible modes of operat=
ion=20
<br>&gt; one requires one piece of configuration
<br>&gt; and one requires the other but it is not valid to specify both.
<br>&gt;
<br>&gt; #include &lt;variant&gt;
<br>&gt; #include &lt;optional&gt;
<br>&gt;
<br>&gt; class Foo
<br>&gt; {
<br>&gt; private:
<br>&gt; =C2=A0=C2=A0=C2=A0 std::variant&lt;int, std::string&gt; bar;
<br>&gt; public:
<br>&gt; =C2=A0=C2=A0=C2=A0 Foo(int a): bar(a) {}
<br>&gt; =C2=A0=C2=A0=C2=A0 Foo(const std::string&amp; b): bar(b) {}
<br>&gt;
<br>&gt; =C2=A0=C2=A0=C2=A0 std::optional&lt;int&gt; getInt() const
<br>&gt; =C2=A0=C2=A0=C2=A0 {
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 std::optional&lt;int&gt=
; ret;
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 auto got =3D std::get_i=
f&lt;int&gt;(&amp;this-&gt;bar);
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (got) ret =3D *got;
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return ret;
<br>&gt; =C2=A0=C2=A0=C2=A0 }
<br>&gt;
<br>&gt; =C2=A0=C2=A0=C2=A0 std::optional&lt;std::string&gt; getString() co=
nst
<br>&gt; =C2=A0=C2=A0=C2=A0 {
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 std::optional&lt;std::s=
tring&gt; ret;
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 auto got =3D std::get_i=
f&lt;std::string&gt;(&amp;<wbr>this-&gt;bar);
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (got) ret =3D *got;
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return ret;
<br>&gt; =C2=A0=C2=A0=C2=A0 }
<br>&gt; };
<br>&gt;
<br>&gt; There seems to be a lot of boilerplate to convert the variant into=
 a=20
<br>&gt; optional for the return.
<br>&gt; There is an inefficiency here in that the type contained has to be=
=20
<br>&gt; checked twice to use this.
<br>&gt;
<br>&gt; There are other ways to do this. You can expose the variant member=
 and=20
<br>&gt; use a visitor
<br>&gt; but using a visitor feels like overkill for this. Also the variant=
 is=20
<br>&gt; an internal implementation detail.
<br>&gt; An equally valid implementation would have two different optional=
=20
<br>&gt; members (but would not enforce their mutual exclusivity).
<br>&gt;
<br>&gt; So do we need (or is there already) something kind of like:
<br>&gt;
<br>&gt; =C2=A0=C2=A0=C2=A0 std::optional&lt;std::string&gt; getString() co=
nst
<br>&gt; =C2=A0=C2=A0=C2=A0 {
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return make_optional&lt=
;std::string&gt;(<wbr>this-&gt;bar);
<br>&gt; =C2=A0=C2=A0=C2=A0 }
<br>&gt;
<br>&gt; or bikeshedding:
<br>&gt;
<br>&gt; =C2=A0=C2=A0=C2=A0 std::optional&lt;std::string&gt; getString() co=
nst
<br>&gt; =C2=A0=C2=A0=C2=A0 {
<br>&gt; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return to_optional&lt;s=
td::string&gt;(this-<wbr>&gt;bar);
<br>&gt; =C2=A0=C2=A0=C2=A0 }
<br>&gt;
<br>
<br>The conversion is simple, but we don&#39;t need to do the conversion un=
til=20
<br>requested. We can see a variant&lt;Ts...&gt; as some kind of ValueOrNon=
e type=20
<br>(that can not change of alternative) once we select one of the=20
<br>alternatives T. Lets name this function select&lt;T&gt;(SumType).
<br>
<br>The conversion would be done only when requested, e.g. by an implicit=
=20
<br>conversion.
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0 optional&lt;T&gt; o =3D select&lt;T&gt;(SumTyp=
e);
<br>
<br>
<br>However, we could as well
<br>
<br>=C2=A0=C2=A0=C2=A0=C2=A0 auto von =3D select&lt;T&gt;(SumType);
<br>
<br>von is not an optional&lt;T&gt;. It is something else, a reference to a=
=20
<br>sum-type, with the perspective of only one of the alternatives. Now we=
=20
<br>can use von as a ValueOrNone, with the usual von.has_value() and *von.=
=20
<br>Note that these functions don&#39;t need to check the type twice:
<br>=C2=A0=C2=A0=C2=A0=C2=A0 von.has_value() should be equivalent to std::g=
et_if&lt;T&gt;(&amp;von) and
<br>=C2=A0=C2=A0=C2=A0=C2=A0 *von equivalent to std::get&lt;T&gt;(von).
<br>
<br>
<br>Just my 2cts.
<br>
<br>Vicente
<br>
<br></blockquote><div>I very much like the name select.<br><br>I see your a=
rgument that a ValueOrNone isn&#39;t strictly an optional but if it isn&#39=
;t don&#39;t we complicate the language<br>with yet another &quot;maybe&quo=
t; type? Wouldn&#39;t the language be cleaner with just one?<br>I guess the=
 answer is adding optional references as previously mentioned?<br><br>Regar=
ds,<br><br>Bruce.<br><br>=C2=A0</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/bbbc65c3-b9e5-4365-9d06-591fcd86d93a%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/bbbc65c3-b9e5-4365-9d06-591fcd86d93a=
%40isocpp.org</a>.<br />

------=_Part_816_571158546.1526553012731--

------=_Part_815_1324439873.1526553012730--

.
