Topic: Idea/Proposal of Const Statements (aka Freezing)


Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Sun, 7 Apr 2019 13:17:19 +1000
Raw View
--0000000000000d592a0585e82978
Content-Type: text/plain; charset="UTF-8"

Here is a quick scribble of an idea I've been toying with...

*Introduction*

We propose const statements.  A const statement makes a variable const.

*Minimal Example*

int main() {
    int x;
    x = 3; // OK
    std::cout << x; // OK
    const x;
    x = 3; // ERROR: x is const
    std::cout << x; // OK
}

*Motivation*

It is a common pattern to modify a variable during the first part of a
function scope (for example, building out some data structure off to the
side) and then intend for it not to be modified for the remainder of the
scope (say, looking up the now filled-out data structure).  In such
situations, usually programmers simply leave the variable non-const for
that remainder, and take the risk of accidental modification - but if a
convieniant way was provided to "freeze" a variable it may mitigate this
risk.

One might argue that the first part of the function could be factored out
into a separate function that returns the variable into a const variable -
or, equivalently a new class type created where the modifying part is
placed into the constructor.

In practice, often the first part of the function that modifies the
variable may not be solely responsible for that side effect, and is folded
with other work.  There may be many variables and a complex algorithm in
play. In many cases such a factoring would be awkward and unnatural.  As
stated previously, we contend that most do not do that, and instead just
leave the variable non-const.

*Specification*

A const statement has the form:

    const N1, N2, ..., Nn;

where each Ni names a variable.  It may only appear at function scope.

Informally, the effect of a const statement is to change the type of each
Ni to the const version of its current type for the remainder of the
function scope.

Formally, perhaps something like:

    using __T = decltype(N);
    __T* __p = &N;
    const __T& N = (*__p);

With the differences that: __T and __p are exposition only, N may shadow a
previous N at the same scope.  N may be a qualified or unqualified name.

Thoughts?

--
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 email 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/CAB%2B4KH%2BkG11_c6Td%3DBDR6DU_2Ubt5R4z60XGKQgyXYTLnWAgUQ%40mail.gmail.com.

--0000000000000d592a0585e82978
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Here is a quick scribble of an idea I&#39;ve been toy=
ing with...</div><div><b><br></b></div><div><b>Introduction</b></div><div><=
b><br></b></div><div>We propose const statements.=C2=A0 A const statement m=
akes a variable const.</div><div><br></div><div><b>Minimal Example</b></div=
><div><br></div><div>int main() {</div><div>=C2=A0 =C2=A0 int x;</div><div>=
=C2=A0 =C2=A0 x =3D 3; // OK</div><div>=C2=A0 =C2=A0 std::cout &lt;&lt; x; =
// OK</div><div>=C2=A0 =C2=A0 const x;</div><div>=C2=A0 =C2=A0 x =3D 3; // =
ERROR: x is const</div><div>=C2=A0 =C2=A0 std::cout &lt;&lt; x;=C2=A0// OK<=
/div><div>}</div><div><br></div><div><b>Motivation</b></div><div><br></div>=
<div>It is a common pattern to modify a variable during the first part of a=
 function scope (for example, building out some data structure off to the s=
ide) and then intend for it not to be modified for the remainder of the sco=
pe (say, looking up the now filled-out data structure).=C2=A0 In such situa=
tions, usually programmers simply leave the variable non-const for that rem=
ainder, and take the risk of accidental modification - but if a convieniant=
 way was provided to &quot;freeze&quot; a variable it may mitigate this ris=
k.</div><div><br></div><div>One might argue that the first part of the func=
tion could be factored out into a separate function that returns the variab=
le into a const variable - or, equivalently a new class type created where =
the modifying part is placed into the constructor.</div><div><br></div><div=
>In practice, often the first part of the function that modifies the variab=
le may not be solely responsible for that side effect, and is folded with o=
ther work.=C2=A0 There may be many variables and a complex algorithm in pla=
y. In many cases such a factoring would be awkward and unnatural.=C2=A0 As =
stated previously, we contend that most do not do that, and instead just le=
ave the variable non-const.</div><div><br></div><div><b>Specification</b></=
div><div><br></div>A const statement has the form:<div><br></div><div>=C2=
=A0 =C2=A0 const N1, N2, ..., Nn;</div><div><br></div><div>where each Ni na=
mes a variable.=C2=A0 It may only appear at function scope.</div><div><br><=
/div><div>Informally, the effect of a const statement is to change the type=
 of each Ni to the const version of its current type for the remainder of t=
he function scope.</div><div><br></div><div>Formally, perhaps something lik=
e:</div><div><br></div><div>=C2=A0 =C2=A0 using __T =3D decltype(N);</div><=
div>=C2=A0 =C2=A0 __T* __p =3D &amp;N;</div><div>=C2=A0 =C2=A0 const __T&am=
p; N =3D (*__p);</div><div><br></div><div>With the differences that: __T an=
d __p are exposition only, N may shadow a previous N at the same scope.=C2=
=A0 N may be a qualified or unqualified name.</div><div><br></div><div>Thou=
ghts?<br></div><div><br></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/CAB%2B4KH%2BkG11_c6Td%3DBDR6DU_2Ubt5R=
4z60XGKQgyXYTLnWAgUQ%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAB%2B4KH%2=
BkG11_c6Td%3DBDR6DU_2Ubt5R4z60XGKQgyXYTLnWAgUQ%40mail.gmail.com</a>.<br />

--0000000000000d592a0585e82978--

.


Author: Alberto Barbati <albertobarbati@gmail.com>
Date: Mon, 8 Apr 2019 01:40:33 -0700 (PDT)
Raw View
------=_Part_1066_762389014.1554712833729
Content-Type: multipart/alternative;
 boundary="----=_Part_1067_187683756.1554712833729"

------=_Part_1067_187683756.1554712833729
Content-Type: text/plain; charset="UTF-8"


Il giorno domenica 7 aprile 2019 05:17:36 UTC+2, Andrew Tomazos ha scritto:
>
> Here is a quick scribble of an idea I've been toying with...
>
> *Introduction*
>
> We propose const statements.  A const statement makes a variable const.
>
> Thoughts?
>
>
Seems a very specific use case for a language feature. Consider that you
already have at least two workarounds with lambdas:

const int x = [&]{
    int x:
    // code that computes x
    return x; }();

// x is now const

and

int x;
// code that computes x

[&, x=x]{
    // x is now const since it's part of closure
}();

Just my two eurocent,

--
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 email 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/fa232fe1-de70-47d4-827c-af8b71fc8b42%40isocpp.org.

------=_Part_1067_187683756.1554712833729
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br>Il giorno domenica 7 aprile 2019 05:17:36 UTC+2, Andre=
w Tomazos ha scritto:<blockquote class=3D"gmail_quote" style=3D"margin: 0;m=
argin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=
=3D"ltr"><div>Here is a quick scribble of an idea I&#39;ve been toying with=
....</div><div><b><br></b></div><div><b>Introduction</b></div><div><b><br></=
b></div><div>We propose const statements.=C2=A0 A const statement makes a v=
ariable const.</div><div><br></div><div>Thoughts?<br></div><div><br></div><=
/div></blockquote><div><br></div><div>Seems a very specific use case for a =
language feature. Consider that you already have at least two workarounds w=
ith lambdas:</div><div><br></div><div> <div style=3D"background-color: rgb(=
250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; bord=
er-width: 1px; overflow-wrap: break-word;" class=3D"prettyprint"><code clas=
s=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008;=
" class=3D"styled-by-prettify">const</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> x </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">[&amp;]{</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=
=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prettify">int</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> x</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">:</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><sp=
an style=3D"color: #800;" class=3D"styled-by-prettify">// code that compute=
s x</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=
=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> x<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}();</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"colo=
r: #800;" class=3D"styled-by-prettify">// x is now const</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br></span></div></code></di=
v><br>and <br></div><div><br></div><div><div style=3D"background-color: rgb=
(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; bor=
der-width: 1px; overflow-wrap: break-word;" class=3D"prettyprint"><code cla=
ss=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008=
;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> x</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br></span><span style=3D"color: #800;" class=3D"styled-by-prettify=
">// code that computes x</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br><br></span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">[&amp;,</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> x</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">]{</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </sp=
an><span style=3D"color: #800;" class=3D"styled-by-prettify">// x is now co=
nst since it&#39;s part of closure</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}();</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br></span></div></code></div></div><div><br>Just my two euroce=
nt,<br></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/fa232fe1-de70-47d4-827c-af8b71fc8b42%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/fa232fe1-de70-47d4-827c-af8b71fc8b42=
%40isocpp.org</a>.<br />

------=_Part_1067_187683756.1554712833729--

------=_Part_1066_762389014.1554712833729--

.