www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - From CppNow 2013

reply "bearophile" <bearophileHUGS lycos.com> writes:
Among the CppNow 2013 slide packs I have found two interesting 
things:

https://github.com/boostcon/cppnow_presentations_2013

----------------------

One is from "Concepts Lite: Constraining Templates with 
Predicates" by Andrew Suttoon, Bjarne Stroustrup et al:

https://github.com/boostcon/cppnow_presentations_2013/blob/master/thu/concepts-lite.pdf?raw=true

This is a D version of C++13 code that uses Concepts Lite:

void sort(C)(C cont) if (SortableContainer!C) {}

They suggest to allow a syntax like this, usable when the 
template constraint has only one argument:

void sort(SortableContainer cont) {}

It's handy especially for lambdas:

(Regular x) => x == y;


If you have two or more types, they must be the same (if you 
don't want this, you have to use the normal longer syntax):

void foo(isRandomAccessRange a,
          isRandomAccessRange b) {
     static assert(is(typeof(a) == typeof(b))); // true.
}


The total number of concepts they define is quite small:

Equality_comparable
Totally_ordered
Regular
Function
Predicate
Relation
Input_iterator
Forward_iterator
Bidirectional_iterator
Sortable

----------------------

The other nice thing I've seen is here:

"Dynamic, Recursive, Heterogeneous Types in Statically-Typed 
Languages" by Richard Saunders, Clinton Jeffery:

https://github.com/boostcon/cppnow_presentations_2013/blob/master/fri/DynRec.pdf?raw=true

They define Tab, Val and little else that allows them to offer a 
dynamic data structure as similar as possible to the Python dicts:

# Python
d = {'a':1, 'b':2.2, 'c':[1,2.2,'three']}
v = int(d['c'][0])
v +=3
d['c'][2] = v


The C++ syntax they use:

Tab d="{'a':1,'b':2.2,'c':[1,2.2,'three']}";
int v = d("c")(0);
v += 3;
d["c"][2] = v;


Another example in C++:

Tab t = "{'a':{'nest':1}}";
cout << t["a"]["nest"] << endl;
t["a"]["nest"] = 17;

Bye,
bearophile
May 22 2013
next sibling parent reply Timothee Cour <thelastmammoth gmail.com> writes:
--047d7b33ce8c4a4d8404def1c6aa
Content-Type: text/plain; charset=ISO-8859-1

On Wed, May 22, 2013 at 2:11 PM, bearophile <bearophileHUGS lycos.com>wrote:

 Among the CppNow 2013 slide packs I have found two interesting things:

 https://github.com/boostcon/**cppnow_presentations_2013<https://github.com/boostcon/cppnow_presentations_2013>

 ----------------------

 One is from "Concepts Lite: Constraining Templates with Predicates" by
 Andrew Suttoon, Bjarne Stroustrup et al:

 https://github.com/boostcon/**cppnow_presentations_2013/**
 blob/master/thu/concepts-lite.**pdf?raw=true<https://github.com/boostcon/cppnow_presentations_2013/blob/master/thu/concepts-lite.pdf?raw=true>

 This is a D version of C++13 code that uses Concepts Lite:

 void sort(C)(C cont) if (SortableContainer!C) {}

 They suggest to allow a syntax like this, usable when the template
 constraint has only one argument:

 void sort(SortableContainer cont) {}

 It's handy especially for lambdas:

 (Regular x) => x == y;


 If you have two or more types, they must be the same (if you don't want
 this, you have to use the normal longer syntax):

 void foo(isRandomAccessRange a,
          isRandomAccessRange b) {
     static assert(is(typeof(a) == typeof(b))); // true.
 }


 The total number of concepts they define is quite small:

 Equality_comparable
 Totally_ordered
 Regular
 Function
 Predicate
 Relation
 Input_iterator
 Forward_iterator
 Bidirectional_iterator
 Sortable

 ----------------------

 The other nice thing I've seen is here:

 "Dynamic, Recursive, Heterogeneous Types in Statically-Typed Languages" by
 Richard Saunders, Clinton Jeffery:

 https://github.com/boostcon/**cppnow_presentations_2013/**
 blob/master/fri/DynRec.pdf?**raw=true<https://github.com/boostcon/cppnow_presentations_2013/blob/master/fri/DynRec.pdf?raw=true>

 They define Tab, Val and little else that allows them to offer a dynamic
 data structure as similar as possible to the Python dicts:

 # Python
 d = {'a':1, 'b':2.2, 'c':[1,2.2,'three']}
 v = int(d['c'][0])
 v +=3
 d['c'][2] = v


 The C++ syntax they use:

 Tab d="{'a':1,'b':2.2,'c':[1,2.2,'**three']}";
 int v = d("c")(0);
 v += 3;
 d["c"][2] = v;


 Another example in C++:

 Tab t = "{'a':{'nest':1}}";
 cout << t["a"]["nest"] << endl;
 t["a"]["nest"] = 17;

 Bye,
 bearophile

regarding the 1st point: The discussion here http://forum.dlang.org/post/mailman.1006.1370836279.13711.digitalmars-d puremagic.com regarding 2nd point: std.variant provides a basis for doing but doesn't quite work because it doesn't deal with nesting. However I've implemented something that works exactly as required: https://github.com/timotheecour/dtools/blob/master/dtools/util/variant_nested.d auto d=variantTupleNamed("a",1,"b","foo","c",variantTuple(1,2.2,"three")); d["a"]=2; auto v=d["c"][0].get!int;//can coerce to int v+=3; d["c"][0]="other1";//can access nested type d["a"]="other2";//can change type d["a"]=variantTuple(0.0,'e'); d["a"]=10; d["a"]+=2; //read-modify-write works, unlike std.variant : 'Due to limitations in current language, read-modify-write operations op= will not work properly' assert(d.text==`["a":12, "b":foo, "c":[other1, 2.2, three]]`); Pending DIP32 (Uniform tuple syntax), this could improve to: auto d=variant( {a=1,b="foo", c={1,2.2,"three"}} ); Any suggestions? Shouldn't (an improved version of) this be in std.variant ? --047d7b33ce8c4a4d8404def1c6aa Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div><br><div class=3D"gmail_quote">On Wed, May 22, 2013 at 2:11 PM, bearop= hile <span dir=3D"ltr">&lt;<a href=3D"mailto:bearophileHUGS lycos.com" targ= et=3D"_blank">bearophileHUGS lycos.com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex">Among the CppNow 2013 slide packs I have fou= nd two interesting things:<br> <br> <a href=3D"https://github.com/boostcon/cppnow_presentations_2013" target=3D= "_blank">https://github.com/boostcon/<u></u>cppnow_presentations_2013</a><b= r> <br> ----------------------<br> <br> One is from &quot;Concepts Lite: Constraining Templates with Predicates&quo= t; by Andrew Suttoon, Bjarne Stroustrup et al:<br> <br> <a href=3D"https://github.com/boostcon/cppnow_presentations_2013/blob/maste= r/thu/concepts-lite.pdf?raw=3Dtrue" target=3D"_blank">https://github.com/bo= ostcon/<u></u>cppnow_presentations_2013/<u></u>blob/master/thu/concepts-lit= e.<u></u>pdf?raw=3Dtrue</a><br> <br> This is a D version of C++13 code that uses Concepts Lite:<br> <br> void sort(C)(C cont) if (SortableContainer!C) {}<br> <br> They suggest to allow a syntax like this, usable when the template constrai= nt has only one argument:<br> <br> void sort(SortableContainer cont) {}<br> <br> It&#39;s handy especially for lambdas:<br> <br> (Regular x) =3D&gt; x =3D=3D y;<br> <br> <br> If you have two or more types, they must be the same (if you don&#39;t want= this, you have to use the normal longer syntax):<br> <br> void foo(isRandomAccessRange a,<br> =A0 =A0 =A0 =A0 =A0isRandomAccessRange b) {<br> =A0 =A0 static assert(is(typeof(a) =3D=3D typeof(b))); // true.<br> }<br> <br> <br> The total number of concepts they define is quite small:<br> <br> Equality_comparable<br> Totally_ordered<br> Regular<br> Function<br> Predicate<br> Relation<br> Input_iterator<br> Forward_iterator<br> Bidirectional_iterator<br> Sortable<br> <br> ----------------------<br> <br> The other nice thing I&#39;ve seen is here:<br> <br> &quot;Dynamic, Recursive, Heterogeneous Types in Statically-Typed Languages= &quot; by Richard Saunders, Clinton Jeffery:<br> <br> <a href=3D"https://github.com/boostcon/cppnow_presentations_2013/blob/maste= r/fri/DynRec.pdf?raw=3Dtrue" target=3D"_blank">https://github.com/boostcon/= <u></u>cppnow_presentations_2013/<u></u>blob/master/fri/DynRec.pdf?<u></u>r= aw=3Dtrue</a><br> <br> They define Tab, Val and little else that allows them to offer a dynamic da= ta structure as similar as possible to the Python dicts:<br> <br> # Python<br> d =3D {&#39;a&#39;:1, &#39;b&#39;:2.2, &#39;c&#39;:[1,2.2,&#39;three&#39;]}= <br> v =3D int(d[&#39;c&#39;][0])<br> v +=3D3<br> d[&#39;c&#39;][2] =3D v<br> <br> <br> The C++ syntax they use:<br> <br> Tab d=3D&quot;{&#39;a&#39;:1,&#39;b&#39;:2.2,&#39;c&#39;:[1,2.2,&#39;<u></u=
three&#39;]}&quot;;<br>

v +=3D 3;<br> d[&quot;c&quot;][2] =3D v;<br> <br> <br> Another example in C++:<br> <br> Tab t =3D &quot;{&#39;a&#39;:{&#39;nest&#39;:1}}&quot;;<br> cout &lt;&lt; t[&quot;a&quot;][&quot;nest&quot;] &lt;&lt; endl;<br> t[&quot;a&quot;][&quot;nest&quot;] =3D 17;<br> <br> Bye,<br> bearophile<br> </blockquote></div><br></div><div><br></div><div>regarding the 1st point:<d= iv>The discussion here=A0<a href=3D"http://forum.dlang.org/post/mailman.100= 6.1370836279.13711.digitalmars-d puremagic.com" target=3D"_blank">http://fo= rum.dlang.org/post/mailman.1006.1370836279.13711.digitalmars-d puremagic.co= m</a>=A0</div> <div><br></div><div>regarding 2nd point:</div><div>std.variant provides a b= asis for doing but doesn&#39;t quite work because it doesn&#39;t deal with = nesting.</div><div><br></div><div>However I&#39;ve implemented something th= at works exactly as required:</div> <div><br></div><div><a href=3D"https://github.com/timotheecour/dtools/blob/= master/dtools/util/variant_nested.d">https://github.com/timotheecour/dtools= /blob/master/dtools/util/variant_nested.d</a></div><div><br></div><div><div=

uot;foo&quot;,&quot;c&quot;,variantTuple(1,2.2,&quot;three&quot;));</div><d= iv><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>d[&quot= ;a&quot;]=3D2;</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>auto = v=3Dd[&quot;c&quot;][0].get!int;//can coerce to int</div><div><span class= =3D"Apple-tab-span" style=3D"white-space:pre"> </span>v+=3D3;</div><div><sp= an class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>d[&quot;c&quo= t;][0]=3D&quot;other1&quot;;//can access nested type</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>d[&qu= ot;a&quot;]=3D&quot;other2&quot;;//can change type</div><div><span class=3D= "Apple-tab-span" style=3D"white-space:pre"> </span>d[&quot;a&quot;]=3Dvaria= ntTuple(0.0,&#39;e&#39;);</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>d[&qu= ot;a&quot;]=3D10;</div><div><span class=3D"Apple-tab-span" style=3D"white-s= pace:pre"> </span>d[&quot;a&quot;]+=3D2; //read-modify-write works, unlike = std.variant : &#39;Due to limitations in current language, read-modify-writ= e operations op=3D will not work properly&#39;</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>asser= t(d.text=3D=3D`[&quot;a&quot;:12, &quot;b&quot;:foo, &quot;c&quot;:[other1,= 2.2, three]]`);</div></div><div><br></div><div><br></div><div>Pending DIP3= 2 (Uniform tuple syntax), this could improve to:</div> <div>=A0 =A0 =A0 =A0 auto d=3Dvariant( {a=3D1,b=3D&quot;foo&quot;, c=3D{1,2= .2,&quot;three&quot;}} );</div><div><br></div><div>Any suggestions?</div><d= iv><br></div><div>Shouldn&#39;t (an improved version of) this be in std.var= iant ?</div> </div><div><br></div> --047d7b33ce8c4a4d8404def1c6aa--
Jun 12 2013
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 6/12/13 5:38 AM, Timothee Cour wrote:
 However I've implemented something that works exactly as required:

 https://github.com/timotheecour/dtools/blob/master/dtools/util/variant_nested.d

          auto
 d=variantTupleNamed("a",1,"b","foo","c",variantTuple(1,2.2,"three"));

Do calls to variantTupleNamed nest within one another? This seems interesting. We should integrate this with json I/O as well. Andrei
Jun 12 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-06-12 11:38, Timothee Cour wrote:

 Pending DIP32 (Uniform tuple syntax), this could improve to:
          auto d=variant( {a=1,b="foo", c={1,2.2,"three"}} );

That looks a bit like my suggestion for anonymous structs: http://forum.dlang.org/thread/kfbnuc$1cro$1 digitalmars.com -- /Jacob Carlborg
Jun 12 2013
prev sibling next sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
--089e01184b1857e2fa04def89dd6
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Jun 12, 2013 at 5:38 AM, Andrei Alexandrescu <
SeeWebsiteForEmail erdani.org> wrote:

 On 6/12/13 5:38 AM, Timothee Cour wrote:

 However I've implemented something that works exactly as required:

 https://github.com/**timotheecour/dtools/blob/**
 master/dtools/util/variant_**nested.d<https://github.com/timotheecour/dtools/blob/master/dtools/util/variant_nested.d>

          auto
 d=variantTupleNamed("a",1,"b",**"foo","c",variantTuple(1,2.2,"**three"));

Do calls to variantTupleNamed nest within one another? This seems interesting. We should integrate this with json I/O as well. Andrei

Yes, the base type Variant2 nests recursively. I've added this to the unittest: Variant2 a1=0; a1=variantTuple(1,"a"); a1[0]=variantTuple("foo",1.1); auto a2=variantTuple(3,[1]); a1[1]=a2; a1~="foo2"; assert(a1.text==`[[foo, 1.1], [3, [1]], foo2]`); (variantTupleNamed is just a way to construct a named tuple, but works as variantTuple) Further, see my comment "read-modify-write works": I would think this could be applied to std.variant.Variant too. --089e01184b1857e2fa04def89dd6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Wed, Jun 12, 2013 at 5:38 AM, Andrei Alexandrescu <span dir=3D"ltr">&lt;= <a href=3D"mailto:SeeWebsiteForEmail erdani.org" target=3D"_blank">SeeWebsi= teForEmail erdani.org</a>&gt;</span> wrote:<br><div class=3D"gmail_quote"><= blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px= #ccc solid;padding-left:1ex"> <div class=3D"im">On 6/12/13 5:38 AM, Timothee Cour wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> However I&#39;ve implemented something that works exactly as required:<br> <br> <a href=3D"https://github.com/timotheecour/dtools/blob/master/dtools/util/v= ariant_nested.d" target=3D"_blank">https://github.com/<u></u>timotheecour/d= tools/blob/<u></u>master/dtools/util/variant_<u></u>nested.d</a><br> <br> =A0 =A0 =A0 =A0 =A0auto<br> d=3DvariantTupleNamed(&quot;a&quot;,1,&quot;b&quot;,<u></u>&quot;foo&quot;,= &quot;c&quot;,variantTuple(1,2.2,&quot;<u></u>three&quot;));<br> </blockquote> <br></div> Do calls to variantTupleNamed nest within one another? This seems interesti= ng. We should integrate this with json I/O as well.<span class=3D"HOEnZb"><= font color=3D"#888888"><br> <br> Andrei<br> </font></span></blockquote></div><br><div>Yes, the base type Variant2 nests= recursively.</div><div><br></div><div>I&#39;ve added this to the unittest:= </div><div><br></div><div><div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Variant2 a1= =3D0;</div><div> <span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>a1=3Dvari= antTuple(1,&quot;a&quot;);</div><div><span class=3D"Apple-tab-span" style= =3D"white-space:pre"> </span>a1[0]=3DvariantTuple(&quot;foo&quot;,1.1);</d= iv><div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>a= uto a2=3DvariantTuple(3,[1]);</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>a1[1= ]=3Da2;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">= </span>a1~=3D&quot;foo2&quot;;</div><div><span class=3D"Apple-tab-span" s= tyle=3D"white-space:pre"> </span>assert(a1.text=3D=3D`[[foo, 1.1], [3, [1]= ], foo2]`);</div> </div><div><br></div><div>(variantTupleNamed is just a way to construct a n= amed tuple, but works as variantTuple)</div><div><br></div><div>Further, se= e my comment &quot;read-modify-write works&quot;: I would think this could = be applied to std.variant.Variant too.</div> <div><br></div><div><br></div> --089e01184b1857e2fa04def89dd6--
Jun 12 2013
prev sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
--e89a8ff1ce0e60807804def8c1cb
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Jun 12, 2013 at 9:16 AM, Jacob Carlborg <doob me.com> wrote:

 On 2013-06-12 11:38, Timothee Cour wrote:

  Pending DIP32 (Uniform tuple syntax), this could improve to:
          auto d=variant( {a=1,b="foo", c={1,2.2,"three"}} );

That looks a bit like my suggestion for anonymous structs: http://forum.dlang.org/thread/**kfbnuc$1cro$1 digitalmars.com<http://forum.dlang.org/thread/kfbnuc$1cro$1 digitalmars.com> -- /Jacob Carlborg

Hi Jacob, IIRC, I think your anonymous struct suggestion has more to do with DIP32, and in fact DIP32 would make your anonymous structs work. But yes, having such tuple notation would make everything nicer. --e89a8ff1ce0e60807804def8c1cb Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Wed, Jun 12, 2013 at 9:16 AM, Jacob Carlborg <span dir=3D"ltr">&lt;<a hr= ef=3D"mailto:doob me.com" target=3D"_blank">doob me.com</a>&gt;</span> wrot= e:<br><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D= "margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <div class=3D"im">On 2013-06-12 11:38, Timothee Cour wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Pending DIP32 (Uniform tuple syntax), this could improve to:<br> =A0 =A0 =A0 =A0 =A0auto d=3Dvariant( {a=3D1,b=3D&quot;foo&quot;, c=3D{1,2.2= ,&quot;three&quot;}} );<br> </blockquote> <br></div> That looks a bit like my suggestion for anonymous structs:<br> <br> <a href=3D"http://forum.dlang.org/thread/kfbnuc$1cro$1 digitalmars.com" tar= get=3D"_blank">http://forum.dlang.org/thread/<u></u>kfbnuc$1cro$1 digitalma= rs.com</a><span class=3D"HOEnZb"><font color=3D"#888888"><br> <br> -- <br> /Jacob Carlborg<br> </font></span></blockquote></div><br><div>Hi Jacob,=A0</div><div>IIRC, I th= ink your anonymous struct suggestion has more to do with DIP32, and in fact= DIP32 would make your anonymous structs work.</div><div>But yes, having su= ch tuple notation would make everything nicer.</div> <div><br></div> --e89a8ff1ce0e60807804def8c1cb--
Jun 12 2013