www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about eponymous template trick

reply "Uranuz" <neuranuz gmail.com> writes:
I have an example of code like this:

template Node(String)
{
	struct Node {}
	struct Name {}
	struct Attr {}
	
}

void main()
{
	alias MyNode = Node!(string).Node;
	alias MyName = Node!(string).Name;
	alias MyAttr = Node!(string).Attr;
	
}

This code fails during compilation with message:

Compilation output:

/d228/f410.d(12): Error: no property 'Node' for type 
'Node!string' /d228/f410.d(12): Error: no property 'Node' for 
type 'Node!string' /d228/f410.d(13): Error: no property 'Name' 
for type 'Node!string' /d228/f410.d(13): Error: no property 
'Name' for type 'Node!string' /d228/f410.d(14): Error: no 
property 'Attr' for type 'Node!string' /d228/f410.d(14): Error: 
no property 'Attr' for type 'Node!string'

So question is: is this intended behaviour and I'm missing 
something about eponymous templates? Or is it a bug in compiler?
Nov 03 2014
next sibling parent reply "MrSmith" <mrsmith33 yandex.ru> writes:
On Monday, 3 November 2014 at 14:07:55 UTC, Uranuz wrote:
 I have an example of code like this:

 template Node(String)
 {
 	struct Node {}
 	struct Name {}
 	struct Attr {}
 	
 }

 void main()
 {
 	alias MyNode = Node!(string).Node;
 	alias MyName = Node!(string).Name;
 	alias MyAttr = Node!(string).Attr;
 	
 }

 This code fails during compilation with message:

 Compilation output:

 /d228/f410.d(12): Error: no property 'Node' for type 
 'Node!string' /d228/f410.d(12): Error: no property 'Node' for 
 type 'Node!string' /d228/f410.d(13): Error: no property 'Name' 
 for type 'Node!string' /d228/f410.d(13): Error: no property 
 'Name' for type 'Node!string' /d228/f410.d(14): Error: no 
 property 'Attr' for type 'Node!string' /d228/f410.d(14): Error: 
 no property 'Attr' for type 'Node!string'

 So question is: is this intended behaviour and I'm missing 
 something about eponymous templates? Or is it a bug in compiler?
Looks like compiler looks for Node, Name and Attr in Node struct, because of eponymous thing. This code works though: template N(String) { struct Node {} struct Name {} struct Attr {} } void main() { alias MyNode = N!(string).Node; alias MyName = N!(string).Name; alias MyAttr = N!(string).Attr; }
Nov 03 2014
parent reply "Uranuz" <neuranuz gmail.com> writes:
 Looks like compiler looks for Node, Name and Attr in Node 
 struct, because of eponymous thing.
I understand it but I want to know if it is documented behaviour or not. Could anybody clear what happens with eponymous stuff and why I can't get acces to *other* declarations inside eponymous template? I guess that it is not intended behaviour.
Nov 03 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/03/2014 06:36 AM, Uranuz wrote:
 Looks like compiler looks for Node, Name and Attr in Node struct,
 because of eponymous thing.
I understand it but I want to know if it is documented behaviour or not. Could anybody clear what happens with eponymous stuff and why I can't get acces to *other* declarations inside eponymous template? I guess that it is not intended behaviour.
I think it's the intended behavior. I think documentation is outdated. Ali
Nov 03 2014
next sibling parent "Uranuz" <neuranuz gmail.com> writes:
 I think it's the intended behavior. I think documentation is 
 outdated.

 Ali
Thanks. So I will modify my programme to workaround this.
Nov 03 2014
prev sibling next sibling parent "Uranuz" <neuranuz gmail.com> writes:
Also I failed to find any documentation about eponymous stuff in 
language reference. As far as I remember it was here but now 
looks like it is missing.
Nov 03 2014
prev sibling parent "Sean Kelly" <sean invisibleduck.org> writes:
On Monday, 3 November 2014 at 14:58:03 UTC, Ali Çehreli wrote:
 I think it's the intended behavior. I think documentation is 
 outdated.
Both forms should really work though. I had always thought that the short form was simply possible if the names matched.
Nov 03 2014
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/3/14 9:07 AM, Uranuz wrote:
 I have an example of code like this:

 template Node(String)
 {
      struct Node {}
      struct Name {}
      struct Attr {}

 }

 void main()
 {
      alias MyNode = Node!(string).Node;
      alias MyName = Node!(string).Name;
      alias MyAttr = Node!(string).Attr;

 }

 This code fails during compilation with message:

 Compilation output:

 /d228/f410.d(12): Error: no property 'Node' for type 'Node!string'
 /d228/f410.d(12): Error: no property 'Node' for type 'Node!string'
 /d228/f410.d(13): Error: no property 'Name' for type 'Node!string'
 /d228/f410.d(13): Error: no property 'Name' for type 'Node!string'
 /d228/f410.d(14): Error: no property 'Attr' for type 'Node!string'
 /d228/f410.d(14): Error: no property 'Attr' for type 'Node!string'

 So question is: is this intended behaviour and I'm missing something
 about eponymous templates? Or is it a bug in compiler?
This is troubling. So you can never access struct Name inside there? This USED to work IIRC, but then again, the eponymous trick only used to work if there was only one member in the template. -Steve
Nov 03 2014