From CppNow 2013

Timothee Cour thelastmammoth at gmail.com
Wed Jun 12 02:38:22 PDT 2013


On Wed, May 22, 2013 at 2:11 PM, bearophile <bearophileHUGS at 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 ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130612/e1315df3/attachment-0001.html>


More information about the Digitalmars-d mailing list