www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Well that was stupid of me (feature request)

reply BCS <ao pathlink.com> writes:
I just came across a stupid little mistake I made that probably added a few 
dozen K to may app.

template Foo(T)
{
	int Bar()
	{
		struct Baz {int i; char j}
		Stack!(Baz) newton;
	}
}

struct Stack(T)
{
	unittest
	{
		writef("%s\n", T.stringof);
	}
}

Look at that for a few seconds. Do you see it?




Well I didn't at first. In fact it wasn't until I added the status report 
line to my unittest that I notice the problem.
What I saw was about 10 time too many copies of the unittest running. And 
all on the same type. After staring at it for a while (and eating dinner, 
and doing some homework) I figured it out.
The Baz type is not one type, but one type for /every/ version of the Foo 
template. Therefor you get a different (identical) version of the Stack
template 
for every copy of the Foo template.

The solution is easy in this case. Move Baz out of Foo. However What would 
be nice is a way to say; "This thing that is inside of a template is the 
same thing for all versions of the template".

Note: if you want a type to be reused for only some of the template args, 
you could make it a template marked for reuses but specialize it on the args 
you want to keep.
Sep 10 2007
parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
BCS wrote:
 I just came across a stupid little mistake I made that probably added a 
 few dozen K to may app.
 
 template Foo(T)
 {
     int Bar()
     {
         struct Baz {int i; char j}
         Stack!(Baz) newton;
     }
 }
 
 struct Stack(T)
 {
     unittest
     {
         writef("%s\n", T.stringof);
     }
 }
 
That code does compile: it has syntax errors. -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Sep 11 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Bruno,

 BCS wrote:
 
 I just came across a stupid little mistake I made that probably added
 a few dozen K to may app.
 
 template Foo(T)
 {
 int Bar()
 {
 struct Baz {int i; char j}
 Stack!(Baz) newton;
 }
 }
 struct Stack(T)
 {
 unittest
 {
 writef("%s\n", T.stringof);
 }
 }
That code does compile: it has syntax errors.
I never clamed it did compile. In fact the code shown doesn't actually give you anything at all because none of the template are instanced. However that has nothing to do with my point. My point is about a subtle bug in a piece of my code that looks a lot like this.
Sep 11 2007
parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
BCS wrote:
 Reply to Bruno,
 
 BCS wrote:

 I just came across a stupid little mistake I made that probably added
 a few dozen K to may app.

 template Foo(T)
 {
 int Bar()
 {
 struct Baz {int i; char j}
 Stack!(Baz) newton;
 }
 }
 struct Stack(T)
 {
 unittest
 {
 writef("%s\n", T.stringof);
 }
 }
That code does compile: it has syntax errors.
I never clamed it did compile. In fact the code shown doesn't actually give you anything at all because none of the template are instanced. However that has nothing to do with my point. My point is about a subtle bug in a piece of my code that looks a lot like this.
I know it actually has not to do with your point, but you asked for people to look at your code, and see if they could see the mistake. But with a syntax error it would be hard to find the intended mistake without actually reading the part where you say what it is. That's all I'm saying :) -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Sep 11 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Bruno,

 BCS wrote:
 
 Reply to Bruno,
 
 BCS wrote:
 
 I just came across a stupid little mistake I made that probably
 added a few dozen K to may app.
 
 template Foo(T)
 {
   int Bar()
   {
     struct Baz {int i; char j}
     Stack!(Baz) newton;
   }
 }
 struct Stack(T)
 {
   unittest
   {
     writef("%s\n", T.stringof);
   }
 }
That code does compile: it has syntax errors.
I never clamed it did compile. In fact the code shown doesn't actually give you anything at all because none of the template are instanced. However that has nothing to do with my point. My point is about a subtle bug in a piece of my code that looks a lot like this.
I know it actually has not to do with your point, but you asked for people to look at your code, and see if they could see the mistake. But with a syntax error it would be hard to find the intended mistake without actually reading the part where you say what it is. That's all I'm saying :)
<joking> well that assumes they "cheat" and try to compile it. "Real men" don't need a compiler to take on this kind of challenge! </joking> Then again "real men" can do a better job parsing code in there head than "girly men" can do /with/ a compiler. <G>
Sep 11 2007
parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
BCS wrote:
 Reply to Bruno,
 
 BCS wrote:

 Reply to Bruno,

 BCS wrote:

 I just came across a stupid little mistake I made that probably
 added a few dozen K to may app.

 template Foo(T)
 {
   int Bar()
   {
     struct Baz {int i; char j}
     Stack!(Baz) newton;
   }
 }
 struct Stack(T)
 {
   unittest
   {
     writef("%s\n", T.stringof);
   }
 }
That code does compile: it has syntax errors.
I never clamed it did compile. In fact the code shown doesn't actually give you anything at all because none of the template are instanced. However that has nothing to do with my point. My point is about a subtle bug in a piece of my code that looks a lot like this.
I know it actually has not to do with your point, but you asked for people to look at your code, and see if they could see the mistake. But with a syntax error it would be hard to find the intended mistake without actually reading the part where you say what it is. That's all I'm saying :)
<joking> well that assumes they "cheat" and try to compile it. "Real men" don't need a compiler to take on this kind of challenge! </joking> Then again "real men" can do a better job parsing code in there head than "girly men" can do /with/ a compiler. <G>
I noticed your syntax error without compiling. My point maintains. :) -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Sep 11 2007
parent BCS <ao pathlink.com> writes:
Reply to Bruno,

 BCS wrote:
 

 
 Then again "real men" can do a better job parsing code in there head
 than "girly men" can do /with/ a compiler. <G>
 
I noticed your syntax error without compiling. My point maintains. :)
8-O ... <g>
Sep 11 2007