Thread overview
nested templates using Complex! with slices, ... confused, I am!
Aug 09, 2021
james.p.leblanc
Aug 09, 2021
H. S. Teoh
Aug 09, 2021
Paul Backus
Aug 09, 2021
james.p.leblanc
August 09, 2021

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

August 09, 2021
On Mon, Aug 09, 2021 at 06:35:56PM +0000, james.p.leblanc via Digitalmars-d-learn wrote: [...]
> > **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;
> > }**

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.
August 09, 2021

On Monday, 9 August 2021 at 18:35:56 UTC, james.p.leblanc wrote:

> >
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

I think what you want is:

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.

August 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:

> >
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

I think what you want is:

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.

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,
James