Re: Difference between template and mixin template

2019-10-10 Thread Just Dave via Digitalmars-d-learn

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


Difference between template and mixin template

2019-10-10 Thread Just Dave via Digitalmars-d-learn
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?


Re: what is the difference between template and mixin template

2012-06-10 Thread Zhenya

I see.
Thank you,guys)



Re: what is the difference between template and mixin template

2012-06-10 Thread Jonathan M Davis
On Sunday, June 10, 2012 12:05:48 Ali Çehreli wrote:
> On 06/10/2012 10:08 AM, Zhenya wrote:
>  > Hi!Today I completly understood,what I don't now what is the difference
>  > between template and mixin template
> 
> There is a terminology problem: there is no such thing as "mixin
> templates". There are only templates.

There was talk of making it so that only templates marked with mixin can be 
mixed in, since you basically always create templates to be mixed in or not to 
be mixed in rather than having a template possibly being used in either 
scenario. I was thinking that Walter had made that change already, but I guess 
not. I don't know what the current plan with that is.

- Jonathan M Davis


Re: what is the difference between template and mixin template

2012-06-10 Thread Artur Skawina
On 06/10/12 19:45, Zhenya wrote:
> On Sunday, 10 June 2012 at 17:34:19 UTC, Zhenya wrote:
>> I read in documentation,that we have two ways to use mixin statement.
>> 1st: mixin(string_wich_can_be_evaluated_at_compile_time);
>> 2st:mixin template
>> I could'nt find any information about such way to use it
> 
> Also,in this case if we add "mixin" before template nothing will change.
> So I would be grateful if you gave me some example,that help me see the 
> difference,becouse now I think that mixin templates is subset of regular 
> templates

A "normal" template just adds one or more compile-time parameters to a
declaration. Which lets you acccess it as "template_name!(int)" and
"template_name(double)" and the result is the same as if you had written
the contents of template twice, once for ints and another copy for "double".
Nothing more, every "template_name!(int)" refers to the same instance, which
remains in the scope where it is defined.

When you write "mixin template_name!(int)" then that templates content is
copied instead, just if you copy-and-pasted it from where it's defined into
the current scope and substituted "int" for the parameter.

The compiler will also let you mixin a "normal" template, which is then
treated just like it was a mixin-template, which may be confusing at first;
this is why your program didn't fail to compile.
The other way won't work BTW, you cannot use a mixin-template w/o mixin it
in. 

artur


Re: what is the difference between template and mixin template

2012-06-10 Thread Ali Çehreli

On 06/10/2012 10:08 AM, Zhenya wrote:
> Hi!Today I completly understood,what I don't now what is the difference
> between template and mixin template

There is a terminology problem: there is no such thing as "mixin 
templates". There are only templates.


D also has the mixin feature with two flavors:

- Mixing in template instantiations as code (this is called "template 
mixins")


- Mixing in strings as code (this is called "string mixins")

>,becouse I think that this should'nt
> work.But compiler is disagree.Could anybody explain me please?
>
> import std.stdio;
>
> int x;
> template smth()
> {
> void smth(){x = 1;}
> }

That is not a very good example because it happens to be an eponymous 
template.


> void main()
> {
> int x;
> mixin smth;//why it compiles? smth is a regular template

You are instantiating smth, effectively inserting a smth() function 
definition right at this point in code.


> smth();
> writeln(.x);
> writeln(x);
> readln();
> }

Here is another example from a yet-untranslated chapter of mine:

template PointArrayFeature(T, size_t count)
{
import std.stdio;

T[count] points;

void setPoint(size_t index, T point)
{
points[index] = point;
}

void printPoints()
{
writeln("All of the points:");

foreach (i, point; points) {
write(i, ":", point, ' ');
}

writeln();
}
}

That template defines a feature that combines three pieces of code:

1) An array of points of any type

2) The function setPoint() as a setter

3) The function printPoints()

Such a feature can be mixed in at any point in the program. For example, 
the Line struct needs two points of type int:


struct Line
{
 mixin PointArrayFeature!(int, 2);
}

The Line struct can in turn be used in the program with all the features 
that it has gained by mixing in the PointArrayFeature template:


void main()
{
auto line = Line();
line.setPoint(0, 100);
line.setPoint(1, 200);
line.printPoints();
}

Ali

--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html



Re: what is the difference between template and mixin template

2012-06-10 Thread Zhenya

On Sunday, 10 June 2012 at 17:34:19 UTC, Zhenya wrote:
I read in documentation,that we have two ways to use mixin 
statement.

1st: mixin(string_wich_can_be_evaluated_at_compile_time);
2st:mixin template
I could'nt find any information about such way to use it


Also,in this case if we add "mixin" before template nothing will 
change.
So I would be grateful if you gave me some example,that help me 
see the difference,becouse now I think that mixin templates is 
subset of regular templates


Re: what is the difference between template and mixin template

2012-06-10 Thread Zhenya
I read in documentation,that we have two ways to use mixin 
statement.

1st: mixin(string_wich_can_be_evaluated_at_compile_time);
2st:mixin template
I could'nt find any information about such way to use it



Re: what is the difference between template and mixin template

2012-06-10 Thread Artur Skawina
On 06/10/12 19:08, Zhenya wrote:
> Hi!Today I completly understood,what I don't now what is the difference 
> between template and mixin template,becouse I think that this should'nt 
> work.But compiler is disagree.Could anybody explain me please?
> 
> import std.stdio;
> 
> int x;
> template smth()
> {
> void smth(){x = 1;}
> }
> 
> void main()
> {
> int x;
> mixin smth;//why it compiles? smth is a regular template

Because the compiler does not disallow it, obviously.
Do you think that it should? If yes - why?

> writeln(.x);
> writeln(x);
> readln();
> }
> 

artur


what is the difference between template and mixin template

2012-06-10 Thread Zhenya
Hi!Today I completly understood,what I don't now what is the 
difference between template and mixin template,becouse I think 
that this should'nt work.But compiler is disagree.Could anybody 
explain me please?


import std.stdio;

int x;
template smth()
{
void smth(){x = 1;}
}

void main()
{
int x;
mixin smth;//why it compiles? smth is a regular template
smth();
writeln(.x);
writeln(x);
readln();
}