Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 22, 2013 From CppNow 2013 | ||||
---|---|---|---|---|
| ||||
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 |
June 12, 2013 Re: From CppNow 2013 | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments:
| 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 ? |
June 12, 2013 Re: From CppNow 2013 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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
|
June 12, 2013 Re: From CppNow 2013 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | 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 |
June 12, 2013 Re: From CppNow 2013 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu Attachments:
| 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.
|
June 12, 2013 Re: From CppNow 2013 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg Attachments:
| 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.
|
Copyright © 1999-2021 by the D Language Foundation