www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why defining alias and not auto when using a template?

reply "Bienlein" <jeti789 web.de> writes:
Hello,

I took some code snippet from some sample D code and modified it 
a bit:

template TCopy(T, V) {
   private int i = 2;
	
   void copy(out T to, out V to2, T from) {
     to = from;
     to2 = from;
     writeln("i: ", i);
   }
}

void main(string[] args)
{
	int x = 2;
	int y = 2;
	alias myCopy = TCopy!(int, int);	
	myCopy.copy(x, y, 37);
	writeln("x: ", x, " y: ", y);
}

My question is now why I have to declare and alias as in

alias myCopy = TCopy!(int, int);

If I define auto instead of alias, it does not compile. My 
question is why defining auto does not work. I would consider 
this more intuitive.

Thanks, Bienlein
Apr 04 2014
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 4 April 2014 at 13:23:48 UTC, Bienlein wrote:
 Hello,

 I took some code snippet from some sample D code and modified 
 it a bit:

 template TCopy(T, V) {
   private int i = 2;
 	
   void copy(out T to, out V to2, T from) {
     to = from;
     to2 = from;
     writeln("i: ", i);
   }
 }

 void main(string[] args)
 {
 	int x = 2;
 	int y = 2;
 	alias myCopy = TCopy!(int, int);	
 	myCopy.copy(x, y, 37);
 	writeln("x: ", x, " y: ", y);
 }

 My question is now why I have to declare and alias as in

 alias myCopy = TCopy!(int, int);

 If I define auto instead of alias, it does not compile. My 
 question is why defining auto does not work. I would consider 
 this more intuitive.

 Thanks, Bienlein
"auto" is used to declare an instance, or an object. "alias" is used to declare a name. What you are currently doing is saying "the function TCopy!(int, int) can now be refered to as myCopy". You aren't actually creating any data.
Apr 04 2014
parent reply "Bienlein" <jeti789 web.de> writes:
 "auto" is used to declare an instance, or an object.
 "alias" is used to declare a name.

 What you are currently doing is saying "the function 
 TCopy!(int, int) can now be refered to as myCopy". You aren't 
 actually creating any data.
All right, thanks. Then I create an instance: auto myCopy = new TCopy!(int, int); alias myCopy = new TCopy!(int, int); Neither nor compiles now. How can? Seems to me a template is not a class in that sense ?!
Apr 04 2014
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 04 Apr 2014 09:35:57 -0400, Bienlein <jeti789 web.de> wrote:

 "auto" is used to declare an instance, or an object.
 "alias" is used to declare a name.

 What you are currently doing is saying "the function TCopy!(int, int)  
 can now be refered to as myCopy". You aren't actually creating any data.
All right, thanks. Then I create an instance: auto myCopy = new TCopy!(int, int); alias myCopy = new TCopy!(int, int); Neither nor compiles now. How can? Seems to me a template is not a class in that sense ?!
A template is not a structure or class, it is a template. It's basically lexically checked, but not semantically. Once you define it's types and parameters, then the compiler basically substitutes those in, and compiles the result. Note that a class template: class C(T) { } is short for this: template C(T) { class C { } } -Steve
Apr 04 2014
prev sibling parent "anonymous" <anonymous example.com> writes:
On Friday, 4 April 2014 at 13:23:48 UTC, Bienlein wrote:
 template TCopy(T, V) {
[...]
 }
On Friday, 4 April 2014 at 13:35:58 UTC, Bienlein wrote:
 auto myCopy = new TCopy!(int, int);	
 alias myCopy = new TCopy!(int, int);	

 Neither nor compiles now. How can? Seems to me a template is 
 not a class in that sense ?!
Templates are not classes, yes. You can make a class template: class TCopy(T, V) {...} The template instance TCopy!(int, int) then is a class type which can be newed, etc. The same works with structs, functions, interfaces. Add some (template) parameters and it becomes a template that instantiates to the corresponding entity.
Apr 04 2014
prev sibling parent "Frustrated" <Who where.com> writes:
On Friday, 4 April 2014 at 13:23:48 UTC, Bienlein wrote:
 Hello,

 I took some code snippet from some sample D code and modified 
 it a bit:

 template TCopy(T, V) {
   private int i = 2;
 	
   void copy(out T to, out V to2, T from) {
     to = from;
     to2 = from;
     writeln("i: ", i);
   }
 }

 void main(string[] args)
 {
 	int x = 2;
 	int y = 2;
 	alias myCopy = TCopy!(int, int);	
 	myCopy.copy(x, y, 37);
 	writeln("x: ", x, " y: ", y);
 }

 My question is now why I have to declare and alias as in

 alias myCopy = TCopy!(int, int);

 If I define auto instead of alias, it does not compile. My 
 question is why defining auto does not work. I would consider 
 this more intuitive.

 Thanks, Bienlein
When you type TCopy!(int, int) you are not creating an expression with a return variable. TCopy!(int, int) has no return variable so auto can't work(What would be the return value?). All TCopy is, is a compile time container that contains a variable i and a method copy. It contains no return value, hence auto can't work. Now, TCopy!(int, int).i or TCopy!(int, int).copy are "things" that are in the template that can be used. About the only thing you could do is use an eponymous template like
 template TCopy(T, V) {
   private int i = 2;
 	
   void copy(out T to, out V to2, T from) {
     to = from;
     to2 = from;
     writeln("i: ", i);
   }
T TCopy() { return null; }
 }
then auto x = TCopy!(int, int)(someTvalue); then x would be of type T, the return value of the TCopy function(not the template because it doesn't have a return value).
Apr 04 2014