www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Difference between template and mixin template

reply Just Dave <abcdef 1234.com> writes:
I'm trying to get my head around mixing templates. I'm using it 
as kind of a replacement for class inheritance as it seems to fit 
better composition over inheritance. So I do something like:

     mixin template NumberTemplate()
     {
     private:
         int number = 0;
     public:
         int getNumber(int number)
         {
             return number;
         }
     }

     interface INumber
     {
         getNumber(int number);
     }

     class Number : INumber
     {
         template NumberTemplate;
     };

So two questions:

a) Is this correct usage?

b) It compiles if I just do:

     template NumberTemplate()
     {
     private:
         int number = 0;
     public:
         int getNumber(int number)
         {
             return number;
         }
     }

what is the difference between template and mixin template?
Oct 10 2019
parent Just Dave <abcdef 1234.com> writes:
On Thursday, 10 October 2019 at 15:56:36 UTC, Just Dave wrote:
 I'm trying to get my head around mixing templates. I'm using it 
 as kind of a replacement for class inheritance as it seems to 
 fit better composition over inheritance. So I do something like:

     mixin template NumberTemplate()
     {
     private:
         int number = 0;
     public:
         int getNumber(int number)
         {
             return number;
         }
     }

     interface INumber
     {
         getNumber(int number);
     }

     class Number : INumber
     {
         template NumberTemplate;
     };

 So two questions:

 a) Is this correct usage?

 b) It compiles if I just do:

     template NumberTemplate()
     {
     private:
         int number = 0;
     public:
         int getNumber(int number)
         {
             return number;
         }
     }

 what is the difference between template and mixin template?
Sorry I messed up the above code example the following should look like: class Number : INumber { mixin NumberTemplate; };
Oct 10 2019