<div><br><div class="gmail_quote">On Wed, May 22, 2013 at 2:11 PM, bearophile <span dir="ltr"><<a href="mailto:bearophileHUGS@lycos.com" target="_blank">bearophileHUGS@lycos.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Among the CppNow 2013 slide packs I have found two interesting things:<br>
<br>
<a href="https://github.com/boostcon/cppnow_presentations_2013" target="_blank">https://github.com/boostcon/<u></u>cppnow_presentations_2013</a><br>
<br>
----------------------<br>
<br>
One is from "Concepts Lite: Constraining Templates with Predicates" by Andrew Suttoon, Bjarne Stroustrup et al:<br>
<br>
<a href="https://github.com/boostcon/cppnow_presentations_2013/blob/master/thu/concepts-lite.pdf?raw=true" target="_blank">https://github.com/boostcon/<u></u>cppnow_presentations_2013/<u></u>blob/master/thu/concepts-lite.<u></u>pdf?raw=true</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 constraint has only one argument:<br>
<br>
void sort(SortableContainer cont) {}<br>
<br>
It's handy especially for lambdas:<br>
<br>
(Regular x) => x == y;<br>
<br>
<br>
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):<br>
<br>
void foo(isRandomAccessRange a,<br>
         isRandomAccessRange b) {<br>
    static assert(is(typeof(a) == 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've seen is here:<br>
<br>
"Dynamic, Recursive, Heterogeneous Types in Statically-Typed Languages" by Richard Saunders, Clinton Jeffery:<br>
<br>
<a href="https://github.com/boostcon/cppnow_presentations_2013/blob/master/fri/DynRec.pdf?raw=true" target="_blank">https://github.com/boostcon/<u></u>cppnow_presentations_2013/<u></u>blob/master/fri/DynRec.pdf?<u></u>raw=true</a><br>


<br>
They define Tab, Val and little else that allows them to offer a dynamic data structure as similar as possible to the Python dicts:<br>
<br>
# Python<br>
d = {'a':1, 'b':2.2, 'c':[1,2.2,'three']}<br>
v = int(d['c'][0])<br>
v +=3<br>
d['c'][2] = v<br>
<br>
<br>
The C++ syntax they use:<br>
<br>
Tab d="{'a':1,'b':2.2,'c':[1,2.2,'<u></u>three']}";<br>
int v = d("c")(0);<br>
v += 3;<br>
d["c"][2] = v;<br>
<br>
<br>
Another example in C++:<br>
<br>
Tab t = "{'a':{'nest':1}}";<br>
cout << t["a"]["nest"] << endl;<br>
t["a"]["nest"] = 17;<br>
<br>
Bye,<br>
bearophile<br>
</blockquote></div><br></div><div><br></div><div>regarding the 1st point:<div>The discussion here <a href="http://forum.dlang.org/post/mailman.1006.1370836279.13711.digitalmars-d@puremagic.com" target="_blank">http://forum.dlang.org/post/mailman.1006.1370836279.13711.digitalmars-d@puremagic.com</a> </div>
<div><br></div><div>regarding 2nd point:</div><div>std.variant provides a basis for doing but doesn't quite work because it doesn't deal with nesting.</div><div><br></div><div>However I've implemented something that works exactly as required:</div>
<div><br></div><div><a href="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>
        auto d=variantTupleNamed("a",1,"b","foo","c",variantTuple(1,2.2,"three"));</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>d["a"]=2;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>auto v=d["c"][0].get!int;//can coerce to int</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>v+=3;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>d["c"][0]="other1";//can access nested type</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>d["a"]="other2";//can change type</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>d["a"]=variantTuple(0.0,'e');</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>d["a"]=10;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>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'</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>assert(d.text==`["a":12, "b":foo, "c":[other1, 2.2, three]]`);</div></div><div><br></div><div><br></div><div>Pending DIP32 (Uniform tuple syntax), this could improve to:</div>
<div>        auto d=variant( {a=1,b="foo", c={1,2.2,"three"}} );</div><div><br></div><div>Any suggestions?</div><div><br></div><div>Shouldn't (an improved version of) this be in std.variant ?</div>
</div><div><br></div>