Re: accessing numeric template parameters

2014-11-05 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn

On Monday, 3 November 2014 at 21:17:09 UTC, Philippe Sigaud via
Digitalmars-d-learn wrote:

struct polynomial(uint base)
{
private:
   uint[] N;
public:
   this(uint x) { base = x; }


base is part of the type. polynomial is just a 'recipe' for a 
type,
the real struct would be Polynomial!(0), Polynomial!(1), etc. 
Note

that Polynomial!0, Polynomial!1, ... are all different types.

Yes, that's what I intend.


Being part of the type means it's defined only at compile-time, 
you

cannot use a runtime value (like 'x') to initialize it.

Note that with your current code, `base' is not visible outside
Polynomial. You can alias it to a field to make it visible:

struct Polynomial(uint base)
{
alias b = base; // b is visible outside (but set at


Ah, ok. Thank you!


compile-time !)
...
}

You can create one like this:

Polynomial!2 poly;
poly.N = [0,1,0,0,1,1];

Ok, now I remember, struct doesn't need an explicit constructor.
(in this case)


Re: accessing numeric template parameters

2014-11-03 Thread Philippe Sigaud via Digitalmars-d-learn
On Mon, Nov 3, 2014 at 3:27 PM, Dominikus Dittes Scherkl via
Digitalmars-d-learn  wrote:
> If I have a struct with numeric template parameter, how can I access it
> within member functions? Like normal member variables? And how about the
> constructor?
>
> struct polynomial(uint base)
> {
> private:
>uint[] N;
> public:
>this(uint x) { base = x; }

base is part of the type. polynomial is just a 'recipe' for a type,
the real struct would be Polynomial!(0), Polynomial!(1), etc. Note
that Polynomial!0, Polynomial!1, ... are all different types.

Being part of the type means it's defined only at compile-time, you
cannot use a runtime value (like 'x') to initialize it.

Note that with your current code, `base' is not visible outside
Polynomial. You can alias it to a field to make it visible:

struct Polynomial(uint base)
{
alias b = base; // b is visible outside (but set at compile-time !)
...
}

You can create one like this:

Polynomial!2 poly;
poly.N = [0,1,0,0,1,1];

assert(poly.b == 2);

Of course, you cannot change b: `poly.b = 3;' is forbidden.


Re: accessing numeric template parameters

2014-11-03 Thread MrSmith via Digitalmars-d-learn
On Monday, 3 November 2014 at 14:27:47 UTC, Dominikus Dittes 
Scherkl wrote:
If I have a struct with numeric template parameter, how can I 
access it within member functions? Like normal member 
variables? And how about the constructor?


struct polynomial(uint base)
{
private:
   uint[] N;
public:
   this(uint x) { base = x; }
   ...
   void add(Polynomial!base P)
   {
  if(N.length < P.N.length) N.length = P.N.length;
  foreach(i; 0..P.N.length)
  {
 N[i] = (N[i]+P.N[i]) % base;
  }
   }
}

This doesn't work for me :-/


You cannot assign to it, because it is only avaliable during 
compilation. Think of it as an immediate value, not variable.


accessing numeric template parameters

2014-11-03 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn
If I have a struct with numeric template parameter, how can I 
access it within member functions? Like normal member variables? 
And how about the constructor?


struct polynomial(uint base)
{
private:
   uint[] N;
public:
   this(uint x) { base = x; }
   ...
   void add(Polynomial!base P)
   {
  if(N.length < P.N.length) N.length = P.N.length;
  foreach(i; 0..P.N.length)
  {
 N[i] = (N[i]+P.N[i]) % base;
  }
   }
}

This doesn't work for me :-/