www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Video: Generic Programming Galore using D Strange Loop 2011

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

Andrei
Apr 11 2012
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 4/11/12, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
Cool talk! And it just occurred to me that you can actually use a static assert on a return type in D, e.g.: import std.traits; import std.algorithm; auto min(T1, T2)(T1 t1, T2 t2) { return t1; // e.g. implementation bug static assert(is(typeof(return) == CommonType!(T1, T2))); } void main() { auto x = min(1, 1.0); } I didn't know this until now. It might help in cases where the return type is a very complicated template instance and you want to enforce the return expression to be of that type while simultaneously using auto as the return type in the function declaration. It does however do this check *after* any implicit conversions to the return type. So, while my first sample correctly won't compile, the following will compile: import std.traits; import std.algorithm; CommonType!(T1, T2) min(T1, T2)(T1 t1, T2 t2) { return t1; static assert(is(typeof(return) == CommonType!(T1, T2))); } void main() { auto x = min(1, 1.0); } It's interesting to think about.
Apr 11 2012
parent "bearophile" <bearophileHUGS lycos.com> writes:
Andrej Mitrovic:

 it just occurred to me that you can actually use a static 
 assert on a return type in D, e.g.:

 import std.traits;
 import std.algorithm;

 auto min(T1, T2)(T1 t1, T2 t2)
 {
     return t1;  // e.g. implementation bug
     static assert(is(typeof(return) == CommonType!(T1, T2)));
 }

 void main() {
     auto x = min(1, 1.0);
 }

 I didn't know this until now.
I'd like to verify the type of what a range yields inside the post-condition of the function, but I can't use code like this, because currently functions with out{} can't use auto as return type: import std.stdio, std.algorithm, std.traits; auto foo(int x) in { assert(x >= 0); } out(result) { static assert(is(ForeachType!(typeof(result)) == int)); } body { return map!(a => a * 2)([1, 2, 3]); } void main() { writeln(foo(1)); } And you have to keep in mind that inside the out{} 'result' is const, and const ranges aren't that useful, you can't even iterate them... Bye, bearophile
Apr 11 2012
prev sibling next sibling parent reply Olivier Pisano <olivier.pisano laposte.net> writes:
Le 11/04/2012 18:23, Andrei Alexandrescu a ιcrit :
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
Great talk ! I am going to start to feel at ease with those is(typeof(...)) constructs ! :) Olivier
Apr 11 2012
parent simendsjo <simendsjo gmail.com> writes:
On Wed, 11 Apr 2012 21:34:11 +0200, Olivier Pisano  =

<olivier.pisano laposte.net> wrote:

 Le 11/04/2012 18:23, Andrei Alexandrescu a =C3=A9crit :
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D=
 Andrei
Great talk ! I am going to start to feel at ease with those =
 is(typeof(...)) constructs ! :)

 Olivier
Don't speak too hastily :) I promise you'll be amazed at the complexitie= s = of the is expression many more times
Apr 11 2012
prev sibling next sibling parent deadalnix <deadalnix gmail.com> writes:
Le 11/04/2012 18:23, Andrei Alexandrescu a ιcrit :
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
You didn't used the new syntax for closures :'( But nice talk. BTW, it seemed strange to me that you mentioned AST manipulation when talking about mixin, when you didn't seemed to see that as important when we were talking here about AOP/attributes. IMO the difference between string and AST for code manipulation is just the same as string vs closure as template parameters.
Apr 11 2012
prev sibling next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 4/11/12 11:23 AM, Andrei Alexandrescu wrote:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D
Destroy on reddit: http://www.reddit.com/r/programming/comments/s4qul/infoq_generic_programming_galore_using_d_video/ Andrei
Apr 11 2012
prev sibling next sibling parent "Eldar Insafutdinov" <e.insafutdinov gmail.com> writes:
On Wednesday, 11 April 2012 at 16:23:48 UTC, Andrei Alexandrescu 
wrote:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
Also on HN: http://news.ycombinator.com/item?id=3829871
Apr 11 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Andrei Alexandrescu:

 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D
It was a quite good talk. Slide 13: very good, I am asking for min([1, 3, 5)) for a lot of time :-) (http://d.puremagic.com/issues/show_bug.cgi?id=4705 ). I hope to see those new min/max/argMin/argMax functions in Phobos. I'd also like the mins()/maxs() functions, as explained in Issue 4705, their need is common. I have added some use cases there. (But your code doesn't work with opApply, so it doesn't work with everything as the slide says). ------------------------------ Slide 15: auto m = argmin!((x) { return x.length;})(s); As deadalnix notes, that was a good place to show the new D lambda template syntax and UCFS: auto m = s.argmin!(x => x.length)(); But it also shows why Python (unlike Ruby) uses a free function for length (that calls a __len__ standard method on objects and built-ins), it allows you to write that code with no need of lambdas (it also shows why Python named arguments are nice): m = min(s, key=len) So in D to avoid defining a lambda I sometimes use walkLength as free function (but I have to keep in mind it gives different results on narrow strings!): auto m = s.argmin!walkLength(); And maybe "argMin" name is more fitting in D than "argmin". ---------------------------- [Attention, uncooked ideas ahead] Creating a good min() is not so easy. When I did create the dlibs1 in D1 I didn't know this, so I thought of myself as a not so good enough programmer for finding it not so easy to create the min/max functions :-) At about 40.00 of your talk there was an interesting question and answer. D sees 255 on default as an integer literal: auto x = 255; static assert(is(typeof(x) == int)); But D also accepts this, because D knows this is a valid ubyte literal too, it fits in the ubyte interval: ubyte x = 255; The D compiler even accepts this with no errros or warnings, requiring no cast to assign z: void main(string[] args) { uint x; ubyte y = cast(ubyte)args.length; ubyte z = x % y; } (A bug on this: http://d.puremagic.com/issues/show_bug.cgi?id=7834 ). So another possible desiderata for a 'dream' min() function is this to compile, with no need of a cast in the user code: void main(string[] args) { ubyte u = min(args.length, 255); } To allow this I think D needs some more static introspection/skills. The template of the min() function needs to be informed not just that the second argument is of type int, but also that it's a literal (or value known at compile time witn no need of running compile-time code to compute it, this is a requirement of the way D CTFE works), and it also fits in an ubyte range. The result of that min() is of type size_t, so this is true (so the min of an unsigned short and an unsigned int is an unsigned int type still. Types and their ranges are orthogonal things): auto u = min(args.length, 255); static assert(is(typeof(u) == size_t)); But this also compiles because the compiler knows that despite the result of that min() call is a size_t the compile-time range of that size_t value fits inside a ubyte range too (just like for the built-in literal 255 that the compiler knows has a range that fits in an ubyte too): ubyte u = min(args.length, 255); So I think this kind of code needs a way to tell statically: - The actual statically known range of a compile-time known integral value. - It also needs the ability to _assign_ a statically known range to an integer value. To make things simpler the compiler is not required to prove that this is a correct assignment, it accepts it with an act of faith. Opionally I'd also like a way to tell, from inside the template: - If a template argument is a literal (or it's a value known statically with no need to run compile-time code); - If the result of the function is assigned to a variable or not (if this information is used, then the template instantiates itself again in two variants, according to the boolean result of this query)). I think a first step is to have something like this, to call from user code the interval analysis engine inside the D compiler. I think this just is just a way to expose to user code stuff already existing inside the compiler, so I think this requires only a small amount of compiler code to be added (I it's better for the result to be an interval closed on the right, unlike most other intervals in D): uint x = ...; __traits(interval, x & ubyte.max) ===> TypeTuple!(0, 255) That alone is not enough to implement that "dream min". Because if you do this: void foo(T)(T x) { writeln([__traits(interval, x)]); } void main() { foo(255); } I think it prints this, because the result of interval analysis gets lot when the expression ends or at function call point: [-2147483648, 2147483647] Bye, bearophile
Apr 11 2012
prev sibling next sibling parent Caligo <iteronvexor gmail.com> writes:


"Should work at efficiency comparable to hand-written code"

A version of min() that used 'ref' would probably be faster on large
structs.  Is that a problem?  How do you make the decision to exclude
'ref'?  Do [generic] algorithms always go with value semantics?

On Wed, Apr 11, 2012 at 11:23 AM, Andrei Alexandrescu
<SeeWebsiteForEmail erdani.org> wrote:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
Apr 11 2012
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-04-11 18:23, Andrei Alexandrescu wrote:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
Nice talk. I noted a minor error in the slides. On several occasions you check for the "<" operator but then uses the ">" operator in the actual body of the function/template. -- /Jacob Carlborg
Apr 11 2012
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/11/2012 11:28 PM, Jacob Carlborg wrote:
 On 2012-04-11 18:23, Andrei Alexandrescu wrote:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
Nice talk. I noted a minor error in the slides. On several occasions you check for the "<" operator but then uses the ">" operator in the actual body of the function/template.
I have noticed that too but technically it is correct because if < compiles in D, > will compile too. (They are both backed by the same opCmp()). But I agree that it should have been the > operator in the template constraint for at least for simplicity. Ali
Apr 16 2012
prev sibling next sibling parent reply Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
11.04.2012 20:23, Andrei Alexandrescu написал:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
27:17 It's valid to pass an empty list. Compilation will fail when T.length = 1 for 'x[1]'. Andrei is really looking embarrassed telling this like if he knows it's a lie but he has to say it because something forces him to do it... He waged somebody it? :) -- ДСнис Π’. ШСломовский Denis V. Shelomovskij
Apr 16 2012
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 4/16/12 10:00 AM, Denis Shelomovskij wrote:
 11.04.2012 20:23, Andrei Alexandrescu написал:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
27:17 It's valid to pass an empty list. Compilation will fail when T.length = 1 for 'x[1]'. Andrei is really looking embarrassed telling this like if he knows it's a lie but he has to say it because something forces him to do it... He waged somebody it? :)
I think "lie" would be pushing it. The point made is clear albeit I rendered it clumsily - static if does not attempt to compile the branch not taken. Andrei
Apr 16 2012
parent reply Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
17.04.2012 7:55, Andrei Alexandrescu написал:
 On 4/16/12 10:00 AM, Denis Shelomovskij wrote:
 11.04.2012 20:23, Andrei Alexandrescu написал:
 http://www.infoq.com/presentations/Generic-Programming-Galore-Using-D

 Andrei
27:17 It's valid to pass an empty list. Compilation will fail when T.length = 1 for 'x[1]'. Andrei is really looking embarrassed telling this like if he knows it's a lie but he has to say it because something forces him to do it... He waged somebody it? :)
I think "lie" would be pushing it. The point made is clear albeit I rendered it clumsily - static if does not attempt to compile the branch not taken. Andrei
And I even haven't mentioned my point. These videos are great for D newbies (and also very good for other bastards like me too) and should be listed on dlang.org just like articles because it isn't obvious for a newbie to search through the NG for videos (and, maybe, some videos supersedes older ones). It's pity that a video is read-only and it isn't easy to fix slips of the tongue like this one. Maybe some notes can be added? Anyway, these (Andrei's and Walter's) videos are too good to not list them on the site. -- ДСнис Π’. ШСломовский Denis V. Shelomovskij
Apr 17 2012
parent reply "SomeDude" <lovelydear mailmetrash.com> writes:
On Tuesday, 17 April 2012 at 08:00:36 UTC, Denis Shelomovskij 
wrote:
 It's pity that a video is read-only and it isn't easy to fix 
 slips of the tongue like this one. Maybe some notes can be 
 added?

 Anyway, these (Andrei's and Walter's) videos are too good to 
 not list them on the site.
I've cleaned up the wiki a bit and added them: http://prowiki.org/wiki4d/wiki.cgi?WhySwitch
Apr 18 2012
next sibling parent "Jesse Phillips" <Jessekphillips+D gmail.com> writes:
On Wednesday, 18 April 2012 at 18:41:05 UTC, SomeDude wrote:
 I've cleaned up the wiki a bit and added them:
 http://prowiki.org/wiki4d/wiki.cgi?WhySwitch
And thank you for doing that.
Apr 18 2012
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 4/18/12, SomeDude <lovelydear mailmetrash.com> wrote:
 On Tuesday, 17 April 2012 at 08:00:36 UTC, Denis Shelomovskij
 wrote:
 It's pity that a video is read-only and it isn't easy to fix
 slips of the tongue like this one. Maybe some notes can be
 added?

 Anyway, these (Andrei's and Walter's) videos are too good to
 not list them on the site.
I've cleaned up the wiki a bit and added them: http://prowiki.org/wiki4d/wiki.cgi?WhySwitch
You could actually add them here and link to it: http://prowiki.org/wiki4d/wiki.cgi?Videos I made that page a long while ago but I didn't really know where to link it from, so I just put it in the first tab I found (The D Community: http://prowiki.org/wiki4d/wiki.cgi?NeighborHood)
Apr 18 2012
next sibling parent reply Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
19.04.2012 4:11, Andrej Mitrovic написал:
 On 4/18/12, SomeDude<lovelydear mailmetrash.com>  wrote:
 On Tuesday, 17 April 2012 at 08:00:36 UTC, Denis Shelomovskij
 wrote:
 It's pity that a video is read-only and it isn't easy to fix
 slips of the tongue like this one. Maybe some notes can be
 added?

 Anyway, these (Andrei's and Walter's) videos are too good to
 not list them on the site.
I've cleaned up the wiki a bit and added them: http://prowiki.org/wiki4d/wiki.cgi?WhySwitch
Thank you, SomeDude.
 You could actually add them here and link to it:
 http://prowiki.org/wiki4d/wiki.cgi?Videos
 I made that page a long while ago but I didn't really know where to
 link it from, so I just put it in the first tab I found (The D
 Community: http://prowiki.org/wiki4d/wiki.cgi?NeighborHood)
Damn, I didn't know about this page. And it looks like I'm not alone. D wiki is really a maze for me (or I just don't understand something?). It contains lots of outdated info mixed with some up-to-date info. I thought that D wiki is only for experienced D developers who know what is outdated and what is not, what info does wiki contains and where is this info. So I'd like to see videos on dlang.org. -- ДСнис Π’. ШСломовский Denis V. Shelomovskij
Apr 19 2012
parent "Jesse Phillips" <jessekphillips+D gmail.com> writes:
On Thursday, 19 April 2012 at 09:42:43 UTC, Denis Shelomovskij 
wrote:
 D wiki is really a maze for me (or I just don't understand 
 something?). It contains lots of outdated info mixed with some 
 up-to-date info. I thought that D wiki is only for experienced 
 D developers who know what is outdated and what is not, what 
 info does wiki contains and where is this info.
No, it is most certainly for new D users, and that is the problem. New users don't know what needs updated. Experienced users aren't going to find useful information on the wiki so it isn't "vetted" constantly. Having SomeDude take his inexperience to change the pages in what would have helped him is a great contribution.
Apr 19 2012
prev sibling parent "SomeDude" <lovelydear mailmetrash.com> writes:
On Thursday, 19 April 2012 at 00:11:29 UTC, Andrej Mitrovic wrote:
 On 4/18/12, SomeDude <lovelydear mailmetrash.com> wrote:
 On Tuesday, 17 April 2012 at 08:00:36 UTC, Denis Shelomovskij
 wrote:
 It's pity that a video is read-only and it isn't easy to fix
 slips of the tongue like this one. Maybe some notes can be
 added?

 Anyway, these (Andrei's and Walter's) videos are too good to
 not list them on the site.
I've cleaned up the wiki a bit and added them: http://prowiki.org/wiki4d/wiki.cgi?WhySwitch
You could actually add them here and link to it: http://prowiki.org/wiki4d/wiki.cgi?Videos I made that page a long while ago but I didn't really know where to link it from, so I just put it in the first tab I found (The D Community: http://prowiki.org/wiki4d/wiki.cgi?NeighborHood)
Thank you, I didn't know this page.
Apr 20 2012
prev sibling parent reply "Famous" <none none.net> writes:
I like your talk but how did you come to the conclusion that
min(a) does not make sense?

Cheers,
Famous
Apr 22 2012
parent reply bearophile <bearophileHUGS lycos.com> writes:
Famous:

 I like your talk but how did you come to the conclusion that
 min(a) does not make sense?
Do you mean the min of a single item is the item itself? This is right, but this case is better (more handy) left as function overload to ask for the min of a single given iterable. Bye, bearophile
Apr 23 2012
parent reply "Famous" <none none.net> writes:
On Monday, 23 April 2012 at 16:45:19 UTC, bearophile wrote:
 Do you mean the min of a single item is the item itself?
Yes.
 This is right, but this case is better (more handy) left as 
 function overload to ask for the min of a single given iterable.
Would this transparantly work for an item and a set consisting of one item? Of course, expressing an integer a by min(a) is not straighforward. I do not expect anybody writing this explicitly. Nevertheless, it seems to be a valid case. I was wondering whether it could be useful in generative programming in order to avoid a special case somewhere. Cheers, Famous
Apr 24 2012
parent "bearophile" <bearophileHUGS lycos.com> writes:
Famous:

 bearophile:
 Do you mean the min of a single item is the item itself?
Yes.
 This is right, but this case is better (more handy) left as 
 function overload to ask for the min of a single given 
 iterable.
Would this transparantly work for an item and a set consisting of one item?
I think min(A,B,...) is the min between two or more items. While min(A) is the min of the items of the iterable A. This means: min([1,2,3]) => 1 min([[1,2,3], [1,2,4]]) => [1,2,3] min([[1,2,3]]) => [1,2,3] It works with with two ore more items, and with a iterable that contains one or more items. In theory if you call min(1) it's able to see 1 is not an iterable, so it must be a single item, so it is the min. But I think this is a confusing special casing, that's better to avoid. Bye, bearophile
Apr 24 2012