www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what is the difference between template and mixin template

reply "Zhenya" <zheny list.ru> writes:
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();
}
Jun 10 2012
next sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
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
Jun 10 2012
parent Artur Skawina <art.08.09 gmail.com> writes:
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
Jun 10 2012
prev sibling next sibling parent "Zhenya" <zheny list.ru> writes:
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
Jun 10 2012
prev sibling next sibling parent "Zhenya" <zheny list.ru> writes:
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
Jun 10 2012
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 10 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, June 10, 2012 12:05:48 Ali =C3=87ehreli wrote:
 On 06/10/2012 10:08 AM, Zhenya wrote:
  > Hi!Today I completly understood,what I don't now what is the diffe=

  > between template and mixin template
=20
 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 ca= n be=20 mixed in, since you basically always create templates to be mixed in or= not to=20 be mixed in rather than having a template possibly being used in either= =20 scenario. I was thinking that Walter had made that change already, but = I guess=20 not. I don't know what the current plan with that is. - Jonathan M Davis
Jun 10 2012
prev sibling parent "Zhenya" <zheny list.ru> writes:
I see.
Thank you,guys)
Jun 10 2012