www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template mixins vs alias

reply Andrea Fontana <nospam example.com> writes:
Check this code:
http://dpaste.dzfl.pl/fcf876acbbdc

Structs A and B do the same things, in different way.

Is there any difference/limitation between those?

Andrea
Feb 22 2016
parent reply anonymous <anonymous example.com> writes:
On Monday, 22 February 2016 at 13:35:10 UTC, Andrea Fontana wrote:
 Check this code:
 http://dpaste.dzfl.pl/fcf876acbbdc

 Structs A and B do the same things, in different way.

 Is there any difference/limitation between those?

 Andrea
The mixin variant generates a method. That means, you can reference members of the struct in the function. Silly example: ---- mixin template Test(T) { auto returnX() { return x; } } struct A { int x; mixin Test!int; } ---- With the alias variant you get an alias to a free function, not a method. So you couldn't reference x like above. What's nicer about the alias version is that you see what symbol is being generated. It's obvious that `alias returnInit = returnInitImpl!int;` creates a symbol "returnInit". In the mixin variant, you have to read the template's source to see that.
Feb 22 2016
parent reply Andrea Fontana <nospam example.com> writes:
On Monday, 22 February 2016 at 13:56:19 UTC, anonymous wrote:
 On Monday, 22 February 2016 at 13:35:10 UTC, Andrea Fontana 
 wrote:
 Check this code:
 http://dpaste.dzfl.pl/fcf876acbbdc

 Structs A and B do the same things, in different way.

 Is there any difference/limitation between those?

 Andrea
The mixin variant generates a method. That means, you can reference members of the struct in the function.
Of course, but that's not the case.
 What's nicer about the alias version is that you see what 
 symbol is being generated. It's obvious that `alias returnInit 
 = returnInitImpl!int;` creates a symbol "returnInit". In the 
 mixin variant, you have to read the template's source to see 
 that.
I wonder whether one version generates faster assembly or not.
Feb 22 2016
parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
In your case I would guess with -O -release -inline it would generate 
assambly with same (similar) speed.

But in this case it would be different:

mixin template Test()
{
     int returnInit() { return int.init; }
}


int returnInitImpl() { return int.init; }


class A
{
     mixin Test!(); // add virtual method
}

class B
{
     alias returnInit = returnInitImpl;
}


import std.stdio;

void main()
{
     auto a = new A();
     auto b = new B();

     a.returnInit().writeln;
     b.returnInit().writeln;
}

Dne 22.2.2016 v 15:12 Andrea Fontana via Digitalmars-d-learn napsal(a):
 On Monday, 22 February 2016 at 13:56:19 UTC, anonymous wrote:
 On Monday, 22 February 2016 at 13:35:10 UTC, Andrea Fontana wrote:
 Check this code:
 http://dpaste.dzfl.pl/fcf876acbbdc

 Structs A and B do the same things, in different way.

 Is there any difference/limitation between those?

 Andrea
The mixin variant generates a method. That means, you can reference members of the struct in the function.
Of course, but that's not the case.
 What's nicer about the alias version is that you see what symbol is 
 being generated. It's obvious that `alias returnInit = 
 returnInitImpl!int;` creates a symbol "returnInit". In the mixin 
 variant, you have to read the template's source to see that.
I wonder whether one version generates faster assembly or not.
Feb 22 2016