digitalmars.D.learn - nested templates using Complex! with slices, ... confused, I am!
- james.p.leblanc (14/38) Aug 09 2021 Suppose "foo_double" should return a complex double slice, with
- H. S. Teoh (9/15) Aug 09 2021 Your syntax is wrong; the declaration should be:
- Paul Backus (12/28) Aug 09 2021 I think what you want is:
- james.p.leblanc (9/39) Aug 09 2021 H.S & Paul,
Suppose "foo_double" should return a complex double slice, with double input args. Similarly "foo_float" should return a float slice, with float input args. I thought it should be easy to parameterize with a template taking a SINGLE argument, either a"double" or "float" as follows:import std.stdio; import std.complex; Complex!double[] foo_double(double x, double y){ auto r = [x, x]; auto i = [y, y]; auto z = [ Complex!double(x, y), Complex!double(x,y) ]; return z; } Complex!float[] foo_float(float x, float y){ auto r = [x, x]; auto i = [y, y]; auto z = [ Complex!float(x, y), Complex!float(x,y) ]; return z; } **T[] foo_temp(Complex!T[])(T x, T y){ auto r = [x, x]; auto i = [y, y]; auto z = [ Complex!T(x, y), Complex!T(x,y) ]; return z; }** void main(){ auto yd = foo_double(1.1, 2.2); writeln(yd); ... }But, no ... I am WRONG! I get the message: qqq.d(18): Error: identifier expected for template value parameter Nested templates using Complex! really have me confused. How should I be thinking about such things in order to understand better? Best Regards, James
Aug 09 2021
On Mon, Aug 09, 2021 at 06:35:56PM +0000, james.p.leblanc via Digitalmars-d-learn wrote: [...]Your syntax is wrong; the declaration should be: Complex!T[] foo_temp(T)(T x, T y) { ... } Basically, you're parametrizing on T, and returning an array of a Complex type built upon T. T -- Curiosity kills the cat. Moral: don't be the cat.**T[] foo_temp(Complex!T[])(T x, T y){ auto r = [x, x]; auto i = [y, y]; auto z = [ Complex!T(x, y), Complex!T(x,y) ]; return z; }**
Aug 09 2021
On Monday, 9 August 2021 at 18:35:56 UTC, james.p.leblanc wrote:I think what you want is: ```d Complex!T[] foo_temp(T)(T x, T y) { auto r = [x, x]; auto i = [y, y]; auto z = [ Complex!T(x, y), Complex!T(x,y) ]; return z; } ``` This is what I get when I take one of the non-template versions and replace `float` or `double` with `T`.```d T[] foo_temp(Complex!T[])(T x, T y){ auto r = [x, x]; auto i = [y, y]; auto z = [ Complex!T(x, y), Complex!T(x,y) ]; return z; } ``` void main(){ auto yd = foo_double(1.1, 2.2); writeln(yd); ... }But, no ... I am WRONG! I get the message: qqq.d(18): Error: identifier expected for template value parameter
Aug 09 2021
On Monday, 9 August 2021 at 18:44:34 UTC, Paul Backus wrote:On Monday, 9 August 2021 at 18:35:56 UTC, james.p.leblanc wrote:H.S & Paul, Wow, thanks for the quick replies! It all seems so simple now ... but I just could not see it! I had been completely (and confidently, unfortunately) misunderstanding the syntax of that left most column. Thanks again, JamesI think what you want is: ```d Complex!T[] foo_temp(T)(T x, T y) { auto r = [x, x]; auto i = [y, y]; auto z = [ Complex!T(x, y), Complex!T(x,y) ]; return z; } ``` This is what I get when I take one of the non-template versions and replace `float` or `double` with `T`.```d T[] foo_temp(Complex!T[])(T x, T y){ auto r = [x, x]; auto i = [y, y]; auto z = [ Complex!T(x, y), Complex!T(x,y) ]; return z; } ``` void main(){ auto yd = foo_double(1.1, 2.2); writeln(yd); ... }But, no ... I am WRONG! I get the message: qqq.d(18): Error: identifier expected for template value parameter
Aug 09 2021