www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - 2 simple Q.

reply "Bent Rasmussen" <exo bent-rasmussen.info> writes:
Disclaimer: This is newbieish. First question. I'd like to do the following.

float[3] Torus(float[2] P) {
    ...
}

I can't do that, so what's the "next best thing". I don't mind classes but
it would be nice to be able to prototype it with just arrays in a safe way.

Second question. Is it possible to use templates to parameterize a function
with values. Something like

template Tori(a : float, c : float)
{
    float[3] Torus(float[2] P)
    {
        ...
    }
}

Or should I use a higher-order function with delegates for that?

Bonus question :) - Where can I find a good webresource to read about
templates in general and preferably specifically in D.

Regards,
Bent Rasmussen

(accidentally posted into another subject before :P)
May 14 2004
next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Bent Rasmussen wrote:

 Disclaimer: This is newbieish. First question. I'd like to do the following.
 
 float[3] Torus(float[2] P) {
     ...
 }
 
 I can't do that, so what's the "next best thing". I don't mind classes but
 it would be nice to be able to prototype it with just arrays in a safe way.

You mean the compiler is giving errors you haven't bothered telling us? I'm not sure if fixed arrays are allowed as return types. But dynamic arrays are, so you could use one of them.
 Second question. Is it possible to use templates to parameterize a function
 with values. Something like
 
 template Tori(a : float, c : float)

Not with that syntax, which would mean a template specialisation for a and c being types, and float being chosen for both. template Tori(float a, float c)
 Or should I use a higher-order function with delegates for that?

Probably. In order to use a template, the parameter values have to be given at compile time. If you need to be able to specify them at runtime, you'll need a higher-order function.
 Bonus question :) - Where can I find a good webresource to read about
 templates in general and preferably specifically in D.

http://www.digitalmars.com/d/template.html Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 14 2004
prev sibling parent reply J C Calvarese <jcc7 cox.net> writes:
Bent Rasmussen wrote:
 Disclaimer: This is newbieish. First question. I'd like to do the following.
 
 float[3] Torus(float[2] P) {
     ...
 }
 
 I can't do that, so what's the "next best thing". I don't mind classes but
 it would be nice to be able to prototype it with just arrays in a safe way.

As you noted, the static array could be wrapped in a class. Also, you could wrapped the static array in a struct. A third option is to use a dynamic array instead of using a static array at all. I'd try the dynamic array first. Some example code: int[] someFunction() /* dynamic array */ { int[] i; i.length = 5; i[0] = 1; i[1] = 1; i[2] = 2; i[3] = 3; i[4] = 5; return i; } struct fa_int5 /* struct wrapper */ { int[5] i; /* static array */ } fa_int5 someFunction2() { fa_int5 k; k.i[0] = 1; k.i[1] = 1; k.i[2] = 2; k.i[3] = 3; k.i[4] = 5; return k; } void main() { int[] j = someFunction(); fa_int5 m; m = someFunction2(); int[5] n = m.i; }
 
 Second question. Is it possible to use templates to parameterize a function
 with values. Something like
 
 template Tori(a : float, c : float)
 {
     float[3] Torus(float[2] P)
     {
         ...
     }
 }
 
 Or should I use a higher-order function with delegates for that?
 
 Bonus question :) - Where can I find a good webresource to read about
 templates in general and preferably specifically in D.

I'm relunctant to call it "good", but I think it's better than nothing: http://www.dsource.org/tutorials/index.php?show_topic=Templates (If I understood templates better, I'd probably post more examples.)
 
 Regards,
 Bent Rasmussen
 
 (accidentally posted into another subject before :P)

-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
May 14 2004
parent reply "Bent Rasmussen" <exo bent-rasmussen.info> writes:
 As you noted, the static array could be wrapped in a class. Also, you
 could wrapped the static array in a struct. A third option is to use a
 dynamic array instead of using a static array at all. I'd try the
 dynamic array first.

I see. For this case though the number of elements is so low that it seems most straightforward to use variables for each element.
 I'm relunctant to call it "good", but I think it's better than nothing:
 http://www.dsource.org/tutorials/index.php?show_topic=Templates

Thanks. Templates appear to be a much more powerful tool than I expected. The template problem I had was solved by using syntax given in the first reply. But it did give me another problem; while this compiles template TTorus(int a, int c) { float[] Torus(float[2] P) { float[3] Q; Q[0] = (c + a*cos(P[1]))*cos(P[0]); Q[1] = (c + a*cos(P[1]))*sin(P[0]); Q[2] = a*sin(P[1]); return Q; } } alias TTorus!(1,2) Torus; this does not template TTorus(float a, float c) { float[] Torus(float[2] P) { float[3] Q; Q[0] = (c + a*cos(P[1]))*cos(P[0]); Q[1] = (c + a*cos(P[1]))*sin(P[0]); Q[2] = a*sin(P[1]); return Q; } } alias TTorus!(1.0f,2.0f) Torus; The compiler says Error: integral type expected for value-parameter, not float. So this can't be done with templates, although the reason escapes me. Regards, Bent Rasmussen
May 15 2004
parent Ben Hinkle <bhinkle4 juno.com> writes:
Bent Rasmussen wrote:

 As you noted, the static array could be wrapped in a class. Also, you
 could wrapped the static array in a struct. A third option is to use a
 dynamic array instead of using a static array at all. I'd try the
 dynamic array first.

I see. For this case though the number of elements is so low that it seems most straightforward to use variables for each element.
 I'm relunctant to call it "good", but I think it's better than nothing:
 http://www.dsource.org/tutorials/index.php?show_topic=Templates

Thanks. Templates appear to be a much more powerful tool than I expected. The template problem I had was solved by using syntax given in the first reply. But it did give me another problem; while this compiles template TTorus(int a, int c) { float[] Torus(float[2] P) { float[3] Q; Q[0] = (c + a*cos(P[1]))*cos(P[0]); Q[1] = (c + a*cos(P[1]))*sin(P[0]); Q[2] = a*sin(P[1]); return Q; } } alias TTorus!(1,2) Torus; this does not template TTorus(float a, float c) { float[] Torus(float[2] P) { float[3] Q; Q[0] = (c + a*cos(P[1]))*cos(P[0]); Q[1] = (c + a*cos(P[1]))*sin(P[0]); Q[2] = a*sin(P[1]); return Q; } } alias TTorus!(1.0f,2.0f) Torus; The compiler says Error: integral type expected for value-parameter, not float. So this can't be done with templates, although the reason escapes me. Regards, Bent Rasmussen

The template parameters have to be converted to strings for name mangling. This is easy for integral types but floating point has roundoff error and messy string representations. Plus if non-integral types are allowed then the name mangler would have to start encoding the type of the parameter to distinguish between, for example, _TTorus_1 and _TTorus_1f. So basically I think it is to keep the name mangling and template matching simple.
May 15 2004