Re: nested templates using Complex! with slices, ... confused, I am!

2021-08-09 Thread james.p.leblanc via Digitalmars-d-learn

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:

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


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


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




Re: nested templates using Complex! with slices, ... confused, I am!

2021-08-09 Thread Paul Backus via Digitalmars-d-learn

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

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


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


Re: nested templates using Complex! with slices, ... confused, I am!

2021-08-09 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Aug 09, 2021 at 06:35:56PM +, 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.


nested templates using Complex! with slices, ... confused, I am!

2021-08-09 Thread james.p.leblanc via Digitalmars-d-learn
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