digitalmars.D - Constrained Templates
- Walter Bright (2/2) Jun 13 2010 http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html
- Yao G. (5/7) Jun 13 2010 http://www.reddit.com/r/programming/comments/cel4o/constrained_templates...
- Walter Bright (4/5) Jun 13 2010 Thanks!
- Yao G. (6/11) Jun 13 2010 Maybe I did something wrong at the moment I submitted the article. :(
- Walter Bright (2/3) Jun 13 2010 I sent an email to the reddit moderators asking what's up with that.
- Andrei Alexandrescu (4/8) Jun 13 2010 I posted this as a test:
- Andrei Alexandrescu (13/24) Jun 13 2010 Sorry I was on the plane and in a hurry so I posted to general reddit,
- Yao G. (6/9) Jun 13 2010 It is now displayed in the front page.
- Walter Bright (3/16) Jun 13 2010 Here's the email I received from the moderators: "Fixed, it was autobann...
- Andrei Alexandrescu (4/21) Jun 13 2010 This is odd. Now my post on tech talk tips has vanished from the new
- Yao G. (7/20) Jun 14 2010 :D That's weird. I just filled some typical form controls and clicked
- Leandro Lucarella (42/45) Jun 13 2010 Nice article, but when I read:
- Yao G. (11/49) Jun 13 2010 Andrei used that idiom quite extensive in Phobos, unfortunately, I dare ...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (41/53) Jun 13 2010 The method that I learned from Phobos is not the best, but at least the
- BCS (8/31) Jun 13 2010 this shorter vertion also works:
- Steven E. Harris (16/19) Jun 13 2010 The syntax doesn't bother me as much as the suggestion to repeat details
- Simen kjaeraas (12/18) Jun 14 2010 How's about @optional? Marks an entire function as optional, i.e. will
- Jason House (2/23) Jun 14 2010 Would it be a bug or a feature if bar was never included? As written, ba...
- Simen kjaeraas (5/30) Jun 14 2010 Well spotted. And a very good reason not to have this feature (or to
- Don (13/32) Jun 14 2010 This can be trivially shown to be NP-complete.
- Simen kjaeraas (7/19) Jun 14 2010 But how does the compiler decide whether or not to include that code
- Nick Sabalausky (9/11) Jun 13 2010 "and a line number pointing into the template body. The error is being
- BCS (4/20) Jun 13 2010 --
- Walter Bright (3/9) Jun 13 2010 I wished to avoid quality of implementation issues, and instead focus on...
- Andrei Alexandrescu (4/15) Jun 13 2010 I think Nick does make a valid point: there is access to the error, it
- Nick Sabalausky (9/23) Jun 13 2010 My concern was that C++ users (or other non-D-users) might get the mista...
http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html Anyone want to do the honors and post to reddit, ycombinator, etc. ?
Jun 13 2010
http://www.reddit.com/r/programming/comments/cel4o/constrained_templates_in_the_d_programming/ On Sun, 13 Jun 2010 14:01:16 -0500, Walter Bright <newshound1 digitalmars.com> wrote:http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html Anyone want to do the honors and post to reddit, ycombinator, etc. ?-- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jun 13 2010
Yao G. wrote:http://www.reddit.com/r/programming/comments/cel4o/constrained_templates_i _the_d_programming/Thanks! Anyone know why this doesn't appear on the "new" page? http://www.reddit.com/r/programming/new/
Jun 13 2010
Maybe I did something wrong at the moment I submitted the article. :( Somebody else should try to submit it too. On Sun, 13 Jun 2010 15:15:25 -0500, Walter Bright <newshound1 digitalmars.com> wrote:Yao G. wrote:-- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/http://www.reddit.com/r/programming/comments/cel4o/constrained_templates_in_the_d_programming/Thanks! Anyone know why this doesn't appear on the "new" page? http://www.reddit.com/r/programming/new/
Jun 13 2010
Yao G. wrote:Maybe I did something wrong at the moment I submitted the article. :(I sent an email to the reddit moderators asking what's up with that.
Jun 13 2010
Walter Bright wrote:Yao G. wrote:I posted this as a test: http://www.reddit.com/r/reddit.com/comments/cemtc/tech_talk_tips/ AndreiMaybe I did something wrong at the moment I submitted the article. :(I sent an email to the reddit moderators asking what's up with that.
Jun 13 2010
Andrei Alexandrescu wrote:Walter Bright wrote:Sorry I was on the plane and in a hurry so I posted to general reddit, not programming. I now deleted that (sorry BCS) and now reposted under programming. Vote me up please :o). http://www.reddit.com/r/programming/comments/ceo9s/tech_talk_tips/ The point of the experiment was that Walter had hypothesized it's possible his name is on a scrutiny/spam list. I removed his name from the article and the results are mixed: when I posted in the general reddit community the article didn't make it on the new articles list, however this latest post in /programming/ was listed instantly. In the meantime I saw that Walter's constrained templates article appeared but with a delay. Go figure. AndreiYao G. wrote:I posted this as a test: http://www.reddit.com/r/reddit.com/comments/cemtc/tech_talk_tips/ AndreiMaybe I did something wrong at the moment I submitted the article. :(I sent an email to the reddit moderators asking what's up with that.
Jun 13 2010
It is now displayed in the front page. http://www.reddit.com/r/programming/ On Sun, 13 Jun 2010 15:42:13 -0500, Walter Bright <newshound1 digitalmars.com> wrote:Yao G. wrote:-- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/Maybe I did something wrong at the moment I submitted the article. :(I sent an email to the reddit moderators asking what's up with that.
Jun 13 2010
Yao G. wrote:It is now displayed in the front page. http://www.reddit.com/r/programming/ On Sun, 13 Jun 2010 15:42:13 -0500, Walter Bright <newshound1 digitalmars.com> wrote:Here's the email I received from the moderators: "Fixed, it was autobanned." It didn't say what rule was used to autoban it.Yao G. wrote:Maybe I did something wrong at the moment I submitted the article. :(I sent an email to the reddit moderators asking what's up with that.
Jun 13 2010
Walter Bright wrote:Yao G. wrote:This is odd. Now my post on tech talk tips has vanished from the new articles list. AndreiIt is now displayed in the front page. http://www.reddit.com/r/programming/ On Sun, 13 Jun 2010 15:42:13 -0500, Walter Bright <newshound1 digitalmars.com> wrote:Here's the email I received from the moderators: "Fixed, it was autobanned." It didn't say what rule was used to autoban it.Yao G. wrote:Maybe I did something wrong at the moment I submitted the article. :(I sent an email to the reddit moderators asking what's up with that.
Jun 13 2010
:D That's weird. I just filled some typical form controls and clicked submit. No "autoban" button was clicked. Yao G. On Sun, 13 Jun 2010 20:05:04 -0500, Walter Bright <newshound1 digitalmars.com> wrote:Yao G. wrote:-- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/It is now displayed in the front page. http://www.reddit.com/r/programming/ On Sun, 13 Jun 2010 15:42:13 -0500, Walter Bright <newshound1 digitalmars.com> wrote:Here's the email I received from the moderators: "Fixed, it was autobanned." It didn't say what rule was used to autoban it.Yao G. wrote:Maybe I did something wrong at the moment I submitted the article. :(I sent an email to the reddit moderators asking what's up with that.
Jun 14 2010
Walter Bright, el 13 de junio a las 12:01 me escribiste:http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html Anyone want to do the honors and post to reddit, ycombinator, etc. ?Nice article, but when I read: T gcd(T)(T a, T b) if (is(typeof(a % b))) { ... } Under the presence of such beauty as template constraint syntax is, "is(typeof(a % b))" makes my eyes hurt, and my brain wonder. When you get used to this idiom, it might not look to bad, but I'm seeing people new to D wonder "why in the hell was that syntax used?" while they try to decode what is(typeof()) means. I think is really a shame, all the beauty and clarity gained by the simple "if" of template constraints, is lost with the cryptic is(typeof()) (well, maybe not all, but a significant part). I would love to see this fixed/simplified. For example, by applying the changes in suggested in bug 3702 [1]. This looks much better, and is pretty clear to anyone (even people that doesn't know D): T gcd(T)(T a, T b) if (meta.compiles(a % b)) { ... } I guess at this point this is not going to happen for D2, a real shame :S The, the article made me think that, even when template constraints are useful not only for improving error reporting, they are often used only for that. Since the compiler knows all the operations a template parameter will need to satisfy from the function body, it would be nice to have some sort of way to tell the compiler to write the template constraints for us (the obvious ones at least, there might be other template constraints desired besides the ones the ones the compiler can figure out). This way, the errors can be improved without user intervention. [1] http://d.puremagic.com/issues/show_bug.cgi?id=3702 -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ ---------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------- Salvajes, de traje, me quieren enseƱar Salvajes, de traje, me quieren educar
Jun 13 2010
Andrei used that idiom quite extensive in Phobos, unfortunately, I dare to say. It makes the code a lot harder to read. But the alternative, using __traits(compiles, ... ) is even worse IMO, because even as is a little more obvious, is quite verbose. Something I would like to see with template constraints, is the ability to show text messages, like when static if is used. Yao G. On Sun, 13 Jun 2010 14:36:14 -0500, Leandro Lucarella <llucax gmail.com> wrote:Walter Bright, el 13 de junio a las 12:01 me escribiste:-- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html Anyone want to do the honors and post to reddit, ycombinator, etc. ?Nice article, but when I read: T gcd(T)(T a, T b) if (is(typeof(a % b))) { ... } Under the presence of such beauty as template constraint syntax is, "is(typeof(a % b))" makes my eyes hurt, and my brain wonder. When you get used to this idiom, it might not look to bad, but I'm seeing people new to D wonder "why in the hell was that syntax used?" while they try to decode what is(typeof()) means. I think is really a shame, all the beauty and clarity gained by the simple "if" of template constraints, is lost with the cryptic is(typeof()) (well, maybe not all, but a significant part). I would love to see this fixed/simplified. For example, by applying the changes in suggested in bug 3702 [1]. This looks much better, and is pretty clear to anyone (even people that doesn't know D): T gcd(T)(T a, T b) if (meta.compiles(a % b)) { ... } I guess at this point this is not going to happen for D2, a real shame :S The, the article made me think that, even when template constraints are useful not only for improving error reporting, they are often used only for that. Since the compiler knows all the operations a template parameter will need to satisfy from the function body, it would be nice to have some sort of way to tell the compiler to write the template constraints for us (the obvious ones at least, there might be other template constraints desired besides the ones the ones the compiler can figure out). This way, the errors can be improved without user intervention. [1] http://d.puremagic.com/issues/show_bug.cgi?id=3702
Jun 13 2010
Leandro Lucarella wrote:Walter Bright, el 13 de junio a las 12:01 me escribiste:The method that I learned from Phobos is not the best, but at least the function interface reads better: T gcd(T)(T a, T b) if (supports_modulus!T) { T result; // ... return result; } That makes it clear that T must support the modulus operation. Here is how supports_modulus may be defined: template supports_modulus(T) { const bool supports_modulus = is (typeof( { T a; T b; T c = a % b; }())); } Here are what is at play to get that "named constraint": - The curly braces after the typeof define a closure (delegate?) literal - The body includes how the type should be used to satisfy the 'supports_modulus' constraint - The closure is "executed" with the empty parethesis at the end of it (actually, not executed at all; because the expression is a parameter to typeof, which never executes its argument) - [This is my assumption] typeof produces an invalid type for invalid expressions - One of the uses of the 'is' expression produces a bool result if the type that it receives is not valid - When a template contains just one definition and that definition matches the name of the template, the template instantiation is the same as the definition that it contains. i.e. instead of writing (supports_modulus!T).supports_modulus to mean the bool value, one merely writes supports_modulus!T A lot of D features! :) But once that code structure is accepted as an idiom, it works and gives names to constraints. Alihttp://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html Anyone want to do the honors and post to reddit, ycombinator, etc. ?Nice article, but when I read: T gcd(T)(T a, T b) if (is(typeof(a % b))) { ... } Under the presence of such beauty as template constraint syntax is, "is(typeof(a % b))" makes my eyes hurt, and my brain wonder.
Jun 13 2010
Hello Ali,The method that I learned from Phobos is not the best, but at least the function interface reads better: T gcd(T)(T a, T b) if (supports_modulus!T) { T result; // ... return result; } That makes it clear that T must support the modulus operation. Here is how supports_modulus may be defined: template supports_modulus(T) { const bool supports_modulus = is (typeof( { T a; T b; T c = a % b; }())); }this shorter vertion also works: template supports_modulus(T) { const bool supports_modulus = is (typeof(T.init%T.init) == T); } -- ... <IXOYE><
Jun 13 2010
Leandro Lucarella <llucax gmail.com> writes:When you get used to this idiom, it might not look to bad, but I'm seeing people new to D wonder "why in the hell was that syntax used?" while they try to decode what is(typeof()) means.The syntax doesn't bother me as much as the suggestion to repeat details of the template's implementation. C++'s concepts were going in this direction too, adding code that suffers the same problem as documentation: It can fall out of step with "the real code", and keeping it aligned requires repeating details that should only be stated once -- in "the real code". Here we're debating how to state that some type can participate in a modulus operation, but that we even need to apply this operation to the function's arguments is an internal detail. It could change after the first pass at implementing the function, adopting a different algorithm, and we could wind up continuing to impose requirements on the types that are no longer relevant to the revised implementation. It looks like yet another maintenance burden. -- Steven E. Harris
Jun 13 2010
Leandro Lucarella <llucax gmail.com> wrote:it would be nice to have some sort of way to tell the compiler to write the template constraints for us (the obvious ones at least, there might be other template constraints desired besides the ones the ones the compiler can figure out). This way, the errors can be improved without user intervention.How's about optional? Marks an entire function as optional, i.e. will not be included if it does not compile. struct foo( T ) { optional void bar( ) { // Will not exist if T cannot be flabbergasted. T tmp; t.flabbergast( ); } } -- Simen
Jun 14 2010
Simen kjaeraas Wrote:Leandro Lucarella <llucax gmail.com> wrote:Would it be a bug or a feature if bar was never included? As written, bar should never compile...it would be nice to have some sort of way to tell the compiler to write the template constraints for us (the obvious ones at least, there might be other template constraints desired besides the ones the ones the compiler can figure out). This way, the errors can be improved without user intervention.How's about optional? Marks an entire function as optional, i.e. will not be included if it does not compile. struct foo( T ) { optional void bar( ) { // Will not exist if T cannot be flabbergasted. T tmp; t.flabbergast( ); } } -- Simen
Jun 14 2010
Jason House <jadon.james.house gmail.com> wrote:Simen kjaeraas Wrote:Well spotted. And a very good reason not to have this feature (or to write unit tests, I guess :p ) -- SimenLeandro Lucarella <llucax gmail.com> wrote:Would it be a bug or a feature if bar was never included? As written, bar should never compile...it would be nice to have some sort of way to tell the compiler to write the template constraints for us (the obvious ones at least, there might be other template constraints desired besides the ones the ones the compilercanfigure out). This way, the errors can be improved without user intervention.How's about optional? Marks an entire function as optional, i.e. will not be included if it does not compile. struct foo( T ) { optional void bar( ) { // Will not exist if T cannot be flabbergasted. T tmp; t.flabbergast( ); } } -- Simen
Jun 14 2010
Simen kjaeraas wrote:Leandro Lucarella <llucax gmail.com> wrote:This can be trivially shown to be NP-complete. void bar(T)() { static if ( big_function!T) { T t; t.flabbergast( ); } } Compiler cannot determine if T needs flabbergast(), unless it evaluates big_function. Which can be arbitrarily complicated. We're taking the approach of making the default error message in such cases as helpful as possible.it would be nice to have some sort of way to tell the compiler to write the template constraints for us (the obvious ones at least, there might be other template constraints desired besides the ones the ones the compiler can figure out). This way, the errors can be improved without user intervention.How's about optional? Marks an entire function as optional, i.e. will not be included if it does not compile. struct foo( T ) { optional void bar( ) { // Will not exist if T cannot be flabbergasted. T tmp; t.flabbergast( ); } }
Jun 14 2010
Don <nospam nospam.com> wrote:This can be trivially shown to be NP-complete. void bar(T)() { static if ( big_function!T) { T t; t.flabbergast( ); } } Compiler cannot determine if T needs flabbergast(), unless it evaluates big_function. Which can be arbitrarily complicated.But how does the compiler decide whether or not to include that code usually? Would it not then also have to evaluate big_function?We're taking the approach of making the default error message in such cases as helpful as possible.Which is likely a better choice. The idea just hit me, and I decided to probe for reactions. -- Simen
Jun 14 2010
"Walter Bright" <newshound1 digitalmars.com> wrote in message news:hv39tt$87s$1 digitalmars.com...http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html Anyone want to do the honors and post to reddit, ycombinator, etc. ?"and a line number pointing into the template body. The error is being reported as occurring somewhere other than where the actual problem is" That would be a good place to point out that recent versions of DMD display template instantiation traces upon template instatiation errors (Does C++ do that?). Then, of course, it could go on to "but better yet...". ------------------------------- Not sent from an iPhone.
Jun 13 2010
Hello Nick,"Walter Bright" <newshound1 digitalmars.com> wrote in message news:hv39tt$87s$1 digitalmars.com...I think GCC does. But IIRC it ends up being an almost unreadable one.http://www.drdobbs.com/blog/archives/2010/06/constrained_tem.html Anyone want to do the honors and post to reddit, ycombinator, etc. ?"and a line number pointing into the template body. The error is being reported as occurring somewhere other than where the actual problem is" That would be a good place to point out that recent versions of DMD display template instantiation traces upon template instatiation errors (Does C++ do that?).------------------------------- Not sent from an iPhone.-- ... <IXOYE><
Jun 13 2010
Nick Sabalausky wrote:"and a line number pointing into the template body. The error is being reported as occurring somewhere other than where the actual problem is" That would be a good place to point out that recent versions of DMD display template instantiation traces upon template instatiation errors (Does C++ do that?). Then, of course, it could go on to "but better yet...".I wished to avoid quality of implementation issues, and instead focus on characteristics of the language.
Jun 13 2010
Walter Bright wrote:Nick Sabalausky wrote:I think Nick does make a valid point: there is access to the error, it just takes some more info from the compiler. Andrei"and a line number pointing into the template body. The error is being reported as occurring somewhere other than where the actual problem is" That would be a good place to point out that recent versions of DMD display template instantiation traces upon template instatiation errors (Does C++ do that?). Then, of course, it could go on to "but better yet...".I wished to avoid quality of implementation issues, and instead focus on characteristics of the language.
Jun 13 2010
"Andrei Alexandrescu" <SeeWebsiteForEmail erdani.org> wrote in message news:hv46bv$1kpl$2 digitalmars.com...Walter Bright wrote:My concern was that C++ users (or other non-D-users) might get the mistaken impression that without constraints, *all* they would get is the unhelpful "Error: incompatible types for ((a) % (b)): 'int*' and 'int*'", and re-enforce their belief that templates have an innate tendency to give useless error messages, and that if you use D, constraints are the only help you get. But I may be over-thinking it and worrying about nothing.Nick Sabalausky wrote:I think Nick does make a valid point: there is access to the error, it just takes some more info from the compiler."and a line number pointing into the template body. The error is being reported as occurring somewhere other than where the actual problem is" That would be a good place to point out that recent versions of DMD display template instantiation traces upon template instatiation errors (Does C++ do that?). Then, of course, it could go on to "but better yet...".I wished to avoid quality of implementation issues, and instead focus on characteristics of the language.
Jun 13 2010