Re: How would you do this in D?

2011-05-11 Thread Jonathan M Davis
On 2011-05-11 03:59, Don wrote: > Jens Mueller wrote: > > Jonathan M Davis wrote: > >> On 2011-05-10 18:06, Jose Armando Garcia wrote: > >>> Thanks. I should have read > >>> http://www.digitalmars.com/d/2.0/template.html more carefully. > >>> > >>> "Multiple instantiations of a TemplateDeclaration

Re: How would you do this in D?

2011-05-11 Thread Alexander
On 11.05.2011 20:07, Andrei Alexandrescu wrote: > The macro name itself is polluting the global namespace. OK, this I understand. But I cannot have something like: { static int i = 0; } { static int i = 1; } This produces linker warning: io.o:(.tdata.+0x0): multiple def

Re: How would you do this in D?

2011-05-11 Thread Andrei Alexandrescu
On 05/11/2011 12:07 PM, Alexander wrote: On 11.05.2011 02:52, Andrei Alexandrescu wrote: Note that the macro version has other, arguably larger, drawbacks - e.g. it can't be used as an expression, evaluates the limit multiple times. pollutes the global namespace etc. BTW, I don't get it

Re: How would you do this in D?

2011-05-11 Thread Alexander
On 11.05.2011 02:52, Andrei Alexandrescu wrote: > Note that the macro version has other, arguably larger, drawbacks - e.g. it > can't be used as an expression, evaluates the limit multiple times. pollutes > the global namespace etc. BTW, I don't get it - why *scoped* variable is going to *glo

Re: How would you do this in D?

2011-05-11 Thread so
Just use a static variable inside a function parameterized by __FILE__ and __LINE__. Indeed the drawback is that two every() calls in one line will share that static. I consider it a small matter. If it becomes a bear we may as well add __COLUMN__ or something similar. __COLUMN__ would be a

Re: How would you do this in D?

2011-05-11 Thread Don
Jens Mueller wrote: Jonathan M Davis wrote: On 2011-05-10 18:06, Jose Armando Garcia wrote: Thanks. I should have read http://www.digitalmars.com/d/2.0/template.html more carefully. "Multiple instantiations of a TemplateDeclaration with the same TemplateArgumentList, before implicit conversion

Re: How would you do this in D?

2011-05-11 Thread Jens Mueller
Jonathan M Davis wrote: > On 2011-05-10 18:06, Jose Armando Garcia wrote: > > Thanks. I should have read > > http://www.digitalmars.com/d/2.0/template.html more carefully. > > > > "Multiple instantiations of a TemplateDeclaration with the same > > TemplateArgumentList, before implicit conversions,

Re: How would you do this in D?

2011-05-10 Thread Jonathan M Davis
On 2011-05-10 18:06, Jose Armando Garcia wrote: > Thanks. I should have read > http://www.digitalmars.com/d/2.0/template.html more carefully. > > "Multiple instantiations of a TemplateDeclaration with the same > TemplateArgumentList, before implicit conversions, all will refer to > the same instan

Re: How would you do this in D?

2011-05-10 Thread Daniel Gibson
Am 11.05.2011 02:56, schrieb Daniel Gibson: > > There may be a more clever template-trick, however. What about: void EVERY(int threshold, alias action, string file = __FILE__, int line = __LINE__)() { static int cnt; ++cnt; if(cnt==threshold) { action(); cnt=0; } } void main() {

Re: How would you do this in D?

2011-05-10 Thread Jonathan M Davis
On 2011-05-10 17:55, Jose Armando Garcia wrote: > Good suggestion but I dislike the syntax for mixin template for this > use case. It looks like: > > bool every(string file = __FILE__, int line = __LINE__)(int time) > { >static int counter; >if(++counter > time) counter -= time; > >re

Re: How would you do this in D?

2011-05-10 Thread Jose Armando Garcia
Thanks. I should have read http://www.digitalmars.com/d/2.0/template.html more carefully. "Multiple instantiations of a TemplateDeclaration with the same TemplateArgumentList, before implicit conversions, all will refer to the same instantiation." The default values which are evaluated at the cal

Re: How would you do this in D?

2011-05-10 Thread Daniel Gibson
Am 11.05.2011 02:32, schrieb Jose Armando Garcia: > Hey guys, > > I am trying to create a function that return true or executes > "something" the n-th time it is called from a specific call site. How > would you do that in D? In C we can do that with the help of > pre-processor macros. E.g.: > >

Re: How would you do this in D?

2011-05-10 Thread Jose Armando Garcia
Good suggestion but I dislike the syntax for mixin template for this use case. It looks like: bool every(string file = __FILE__, int line = __LINE__)(int time) { static int counter; if(++counter > time) counter -= time; return counter == 1; } works just fine. Hmmm! On Tue, May 10, 2011

Re: How would you do this in D?

2011-05-10 Thread Andrei Alexandrescu
On 5/10/11 7:32 PM, Jose Armando Garcia wrote: Hey guys, I am trying to create a function that return true or executes "something" the n-th time it is called from a specific call site. How would you do that in D? In C we can do that with the help of pre-processor macros. E.g.: --- #include #de

Re: How would you do this in D?

2011-05-10 Thread Jonathan M Davis
On 2011-05-10 17:32, Jose Armando Garcia wrote: > Hey guys, > > I am trying to create a function that return true or executes > "something" the n-th time it is called from a specific call site. How > would you do that in D? In C we can do that with the help of > pre-processor macros. E.g.: > > --

Re: How would you do this in D?

2011-05-10 Thread Jose Armando Garcia
Also, the D implementation breaks if you do: if(every(x)) { ... } if(every(y)) { ...} All on the same line. On Tue, May 10, 2011 at 9:32 PM, Jose Armando Garcia wrote: > Hey guys, > > I am trying to create a function that return true or executes > "something" the n-th time it is called from a s

How would you do this in D?

2011-05-10 Thread Jose Armando Garcia
Hey guys, I am trying to create a function that return true or executes "something" the n-th time it is called from a specific call site. How would you do that in D? In C we can do that with the help of pre-processor macros. E.g.: --- #include #define EVERY(N, ACTION) { static int counter = 0;