www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - inner templates?

reply dennis luehring <dl.soluz gmx.net> writes:
hi

i've try to write an template wich calculates e

ok here it is - but wait i need a helper template to reduce the 
interface to my needs (just to adjust the precision) - is there any way 
getting rid of these helper template (maybe an inner-template or something)?

--
import std.stdio;

template e_help(double p, double s, double f, int n)
{
   static if( s > p )
     const e_help = e_help!(p, s/n,f+s/n,n+1);
   else
     const e_help = f;
}

// this is the interface i want - but without the e_help template
template e(double p = 1e-14)
{
   const e = e_help!(p, 1, 1, 1);
}

void main()
{
   writefln("e: ", e!());
}
--

ciao dennis
May 24 2007
next sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
dennis luehring wrote:
 hi
 
 i've try to write an template wich calculates e
 
 ok here it is - but wait i need a helper template to reduce the 
 interface to my needs (just to adjust the precision) - is there any way 
 getting rid of these helper template (maybe an inner-template or 
 something)?
 
 -- 
 import std.stdio;
 
 template e_help(double p, double s, double f, int n)
 {
   static if( s > p )
     const e_help = e_help!(p, s/n,f+s/n,n+1);
   else
     const e_help = f;
 }
 
 // this is the interface i want - but without the e_help template
 template e(double p = 1e-14)
 {
   const e = e_help!(p, 1, 1, 1);
 }
 
 void main()
 {
   writefln("e: ", e!());
 }
 -- 
 
 ciao dennis

Default arguments should do it in this case: template e(double p=1e-14, double s=1, double f=1, int n=1) { static if( s > p ) const e = e!(p, s/n, f+s/n, n+1); else const e = f; } -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
May 24 2007
parent reply dennis luehring <dl.soluz gmx.net> writes:
Kirk McDonald schrieb:
 dennis luehring wrote:
 hi

 i've try to write an template wich calculates e

 ok here it is - but wait i need a helper template to reduce the 
 interface to my needs (just to adjust the precision) - is there any 
 way getting rid of these helper template (maybe an inner-template or 
 something)?

 -- 
 import std.stdio;

 template e_help(double p, double s, double f, int n)
 {
   static if( s > p )
     const e_help = e_help!(p, s/n,f+s/n,n+1);
   else
     const e_help = f;
 }

 // this is the interface i want - but without the e_help template
 template e(double p = 1e-14)
 {
   const e = e_help!(p, 1, 1, 1);
 }

 void main()
 {
   writefln("e: ", e!());
 }
 -- 

 ciao dennis

Default arguments should do it in this case: template e(double p=1e-14, double s=1, double f=1, int n=1) { static if( s > p ) const e = e!(p, s/n, f+s/n, n+1); else const e = f; }

i known default arguments - i use one in the e template... :-) my problem is that i don't want the parameters s,f,n in the interface (because they are more internal stuff and someone can use them the wrong way) function based with an recursive inner function to hide the internal stuff... double fe(double p=1e-14) { double fe_help(double p, double s=1, double f=1, int n=1) { if( s > p ) return fe_help(p, s/n,f+s/n,n+1); else return f; } return fe_help(p); } it would be great to write the same code with templates...(normaly i port my code just over from functions to templates - except this code) template fe(double p=1e-14) { template fe_help(double p, double s=1, double f=1, int n=1) { if( s > p ) const fe_help = fe_help!(p, s/n,f+s/n,n+1); else const fe_help = f; } return fe_help!(p); } proposal: we've got inner functions, inner classes - what speaks against inner templates? (except that the d compiler don't know the last one) ciao dennis btw: i know how to write an non recursive e calculator without these dirty interface problems double e(double p = 1e-14) { int n=1; double s=1, f=1; while (s>p) { s=s/n; f=f+s; n=n+1; } return f; } but i want an recursive one
May 24 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
dennis luehring wrote:
 template fe(double p=1e-14)
 {
   template fe_help(double p, double s=1, double f=1, int n=1)
   {
     if( s > p )
       const fe_help = fe_help!(p, s/n,f+s/n,n+1);
     else
       const fe_help = f;
   }
   return fe_help!(p);
 }
 
 proposal: we've got inner functions, inner classes - what speaks against
 inner templates? (except that the d compiler don't know the last one)

We already do. Just change that second-last line to: const fe = fe_help!(p); The problem is that then you have to use it like so: fe!().fe; Since having more than one member in a template breaks the implicit property trick. Of course, you can get around that by doing this: private template fe_help(double p, double s=1, double f=1, int n=1) { if( s > p ) const fe_help = fe_help!(p, s/n,f+s/n,n+1); else const fe_help = f; } template fe(double p=1e-14) { const fe = fe_help!(p); } The "private" will hide the fe_help template from outsiders. If you don't care about that middle blank line, it's even the same number of lines of code! See, what you should be asking for is that private members of a template don't break the implicit property trick thing (no idea what it's "officially" called). But that said, it's a pretty minor problem, and easy to work around. -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
May 24 2007
next sibling parent dennis luehring <dl.soluz gmx.net> writes:
Daniel Keep schrieb:
 
 dennis luehring wrote:
 template fe(double p=1e-14)
 {
   template fe_help(double p, double s=1, double f=1, int n=1)
   {
     if( s > p )
       const fe_help = fe_help!(p, s/n,f+s/n,n+1);
     else
       const fe_help = f;
   }
   return fe_help!(p);
 }

 proposal: we've got inner functions, inner classes - what speaks against
 inner templates? (except that the d compiler don't know the last one)

We already do. Just change that second-last line to: const fe = fe_help!(p); The problem is that then you have to use it like so: fe!().fe; Since having more than one member in a template breaks the implicit property trick. Of course, you can get around that by doing this: private template fe_help(double p, double s=1, double f=1, int n=1) { if( s > p ) const fe_help = fe_help!(p, s/n,f+s/n,n+1); else const fe_help = f; } template fe(double p=1e-14) { const fe = fe_help!(p); } The "private" will hide the fe_help template from outsiders. If you don't care about that middle blank line, it's even the same number of lines of code!

 See, what you should be asking for is that private members of a template
 don't break the implicit property trick thing (no idea what it's
 "officially" called).

did you think an inner template (or a changed implicit property thing) would made D more clean?
 But that said, it's a pretty minor problem, and easy to work around.

your right - but it should be no need for such workaround - maybe in later D versions thx
 
 	-- Daniel
 

May 24 2007
prev sibling parent Carlos Santander <csantander619 gmail.com> writes:
Daniel Keep escribió:
 
 See, what you should be asking for is that private members of a template
 don't break the implicit property trick thing (no idea what it's
 "officially" called).
 
 But that said, it's a pretty minor problem, and easy to work around.
 
 	-- Daniel
 

I think Andrei said that would come at some point. -- Carlos Santander Bernal
May 25 2007
prev sibling parent BCS <ao pathlink.com> writes:
Reply to dennis,

 hi
 
 i've try to write an template wich calculates e
 
 ok here it is - but wait i need a helper template to reduce the
 interface to my needs (just to adjust the precision) - is there any
 way getting rid of these helper template (maybe an inner-template or
 something)?
 
 
 ciao dennis
 

Is this of use? import std.stdio; private template e_help(double p, double s, double f, int n) { static if( s > p ) const e_help = e_help!(p, s/n,f+s/n,n+1); else const e_help = f; } public const double e = e_help!(1e-14, 1, 1, 1); void main() { writefln("e: ", e); }
May 25 2007