digitalmars.D.learn - mixin template
- Dan Killebrew (42/42) Jan 27 2014 What is the difference between:
- Dan Killebrew (5/5) Jan 27 2014 Found this:
- Jesse Phillips (14/19) Feb 01 2014 I'm not sure how your conclusion matches with what Jonathon said.
- Timon Gehr (5/6) Jan 28 2014 mixin template Foo(alias a){ alias Foo=a; }
- Dan Killebrew (13/17) Jan 30 2014 Perhaps I was unclear. What I meant:
- Dan Killebrew (3/9) Jan 30 2014 (besides fail to compile in Timon's reply). I should have said
- Frustrated (49/59) Jan 31 2014 Using a mixin template forces it to be used as a mixin which,
- Dicebot (3/11) Jan 31 2014 It is other way around. `mixin template` guarantees that it won't
What is the difference between: A ----------- template Foo() { int x = 5; } B ------------- mixin template Foo() { int x = 5; } The full example is from the first code sample on http://dlang.org/template-mixin.html Both A and B compile and when run, have the exact same output. So how is 'mixin template' different from 'template'? Full example, slightly modified to actually run: import std.stdio; template Foo() { //mixin template Foo() { int x = 5; } mixin Foo; struct Bar { mixin Foo; } void main() { writefln("x = %d", x); // prints 5 { Bar b; int x = 3; writefln("b.x = %d", b.x); // prints 5 writefln("x = %d", x); // prints 3 { mixin Foo; writefln("x = %d", x); // prints 5 x = 4; writefln("x = %d", x); // prints 4 } writefln("x = %d", x); // prints 3 } writefln("x = %d", x); // prints 5 }
Jan 27 2014
Found this: http://forum.dlang.org/thread/ntuysfcivhbphnhnnvcw forum.dlang.org#post-mailman.1409.1339356130.24740.digitalmars-d-learn:40puremagic.com If what Jonathan says is true, then http://dlang.org/template-mixin.html should be updated: s/mixin template/template/
Jan 27 2014
On Tuesday, 28 January 2014 at 06:35:52 UTC, Dan Killebrew wrote:Found this: http://forum.dlang.org/thread/ntuysfcivhbphnhnnvcw forum.dlang.org#post-mailman.1409.1339356130.24740.digitalmars-d-learn:40puremagic.com If what Jonathan says is true, then http://dlang.org/template-mixin.html should be updated: s/mixin template/template/I'm not sure how your conclusion matches with what Jonathon said. History: a Mixin Template was once a template which was mixed in. The syntax "mixin template {}" did not exist. It was realized that one would not design templates which made sense to mixin or be used as a regular template, so it was suggested to require declaring the mixin explicitly at the template declaration. The "mixin template {}" syntax was added, it seems to have only required the use of mixing in, the original "template {}" was left alone probably for backwards compatibility. Looks like docs were updated to the expected definition, but implementation was not made to match.
Feb 01 2014
On 01/28/2014 04:29 AM, Dan Killebrew wrote:So how is 'mixin template' different from 'template'?mixin template Foo(alias a){ alias Foo=a; } pragma(msg, Foo!2); // error template Bar(alias a){ alias Bar=a; } pragma(msg, Bar!2); // ok
Jan 28 2014
mixin template Foo(alias a){ alias Foo=a; } pragma(msg, Foo!2); // error template Bar(alias a){ alias Bar=a; } pragma(msg, Bar!2); // okPerhaps I was unclear. What I meant: What does 'mixin template' do that 'template' does not? Where would I use 'mixin template'? As far as I can tell, 'mixin template' does nothing new; 'template' is sufficient. Thus, 'mixin template' seems like pointless extra syntax. As I said before, this page http://dlang.org/template-mixin.html could have replaced all instances of 'mixin template' with 'template' and it would compile (I tested all examples) and run the same (perhaps not true? I tested about half). Even the example in TDPL on page 284 works the same when I replace 'mixin template' with 'template': (this is my slightly modified version) http://dpaste.dzfl.pl/b582c899fc3f
Jan 30 2014
On Friday, 31 January 2014 at 06:24:27 UTC, Dan Killebrew wrote:(besides fail to compile in Timon's reply). I should have said "it does nothing helpful."mixin template Foo(alias a){ alias Foo=a; } pragma(msg, Foo!2); // error template Bar(alias a){ alias Bar=a; } pragma(msg, Bar!2); // okAs far as I can tell, 'mixin template' does nothing new;
Jan 30 2014
On Friday, 31 January 2014 at 06:26:16 UTC, Dan Killebrew wrote:On Friday, 31 January 2014 at 06:24:27 UTC, Dan Killebrew wrote:Using a mixin template forces it to be used as a mixin which, sometimes you need. It does something new because it is different. If you drop the mixin and use a template then it is a different construct than than a mixin template. e.g., template X() { static this() { .... } } mixin template Y() { static this() { .... } } class A { mixin X; } class B { mixin Y; } class C { X; // ok because X is a normal template } class D { Y; // error, Y has to be mixed in since it is a mixin template } A and B are the same. C compiles fine. D fails because we are using Y like it was a normal template function not it is not. We might want that because a mixin template and a template have different meanings. One mixes in the code on site and one doesn't. If you write a mixin template and it would fail if it were used as a template then you should make sure to use the mixin in front of it. One could argue that they should have been disjoint, i.e., a normal template should not be able to be mixed in and a mixin template must be mixined. This was not done. If you write a mixin template and it can't or shouldn't be used as a normal template, then use the 'mixin template'. This prevents it from being used as a normal template. If you write a normal template and do not want it to be mixed in, you are out of luck. Not sure if their was a good reason for this or not. (probably ok to use and in some cases very useful to have so it was made optional)(besides fail to compile in Timon's reply). I should have said "it does nothing helpful."mixin template Foo(alias a){ alias Foo=a; } pragma(msg, Foo!2); // error template Bar(alias a){ alias Bar=a; } pragma(msg, Bar!2); // okAs far as I can tell, 'mixin template' does nothing new;
Jan 31 2014
On Friday, 31 January 2014 at 06:24:27 UTC, Dan Killebrew wrote:It is other way around. `mixin template` guarantees that it won't be used for anything else but mixing in.mixin template Foo(alias a){ alias Foo=a; } pragma(msg, Foo!2); // error template Bar(alias a){ alias Bar=a; } pragma(msg, Bar!2); // okPerhaps I was unclear. What I meant: What does 'mixin template' do that 'template' does not? Where would I use 'mixin template'?
Jan 31 2014