www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - PLEASE EXPLAIN THE SPEC: alias type specialization

reply Russell Lewis <webmaster villagersonline.com> writes:
I have a set of templates which take alias parameters, and I want to 
specialize them based on the type of the aliased symbols.

So I looked at 
http://digitalmars.com/d/2.0/template.html#TemplateAliasParameter and 
saw the following grammar (but no matching example):

TemplateAliasParameter:
	alias Identifier
	alias Identifier TemplateAliasParameterSpecialization
	alias Identifier TemplateAliasParameterDefault
	alias Identifier TemplateAliasParameterSpecialization 
TemplateAliasParameterDefault

TemplateAliasParameterSpecialization:
	 : Type

TemplateAliasParameterDefault:
	 = Type


So I wrote this code, which DMD rejects:
	template foo(alias A : int) {}


How is this supposed to work?  Or, is this a DMD bug?

Russ
Mar 11 2008
next sibling parent reply Russell Lewis <webmaster villagersonline.com> writes:
Just so you guys know, I made part of the title of my previous message 
as all-caps in an attempt to make it easy to classify my message.  I 
didn't mean to shout.  But looking at it in my newsreader, I think that 
it didn't work.

I've used all-caps prefixes before that worked well, like "HACK," 
"BRAINSTORM," etc.  But this one had too many words.

Sorry!  No hostile feelings intended!
Mar 11 2008
parent BCS <ao pathlink.com> writes:
Reply to Russell,

 Just so you guys know, I made part of the title of my previous message
 as all-caps in an attempt to make it easy to classify my message.  I
 didn't mean to shout.  But looking at it in my newsreader, I think
 that it didn't work.
 
 I've used all-caps prefixes before that worked well, like "HACK,"
 "BRAINSTORM," etc.  But this one had too many words.
 
 Sorry!  No hostile feelings intended!
 

Ahh. What we all wouldn't give for a yank button... I think most of us have done worse.
Mar 11 2008
prev sibling parent Paul Thompson <p.thompson mailinator.com> writes:
== Quote from Russell Lewis (webmaster villagersonline.com)'s article
 I have a set of templates which take alias parameters, and I want to
 specialize them based on the type of the aliased symbols.
 So I looked at
 http://digitalmars.com/d/2.0/template.html#TemplateAliasParameter and
 saw the following grammar (but no matching example):
 TemplateAliasParameter:
 	alias Identifier
 	alias Identifier TemplateAliasParameterSpecialization
 	alias Identifier TemplateAliasParameterDefault
 	alias Identifier TemplateAliasParameterSpecialization
 TemplateAliasParameterDefault
 TemplateAliasParameterSpecialization:
 	 : Type
 TemplateAliasParameterDefault:
 	 = Type
 So I wrote this code, which DMD rejects:
 	template foo(alias A : int) {}
 How is this supposed to work?  Or, is this a DMD bug?
 Russ

I was working through the grammar and found this too. The document at: http://www.digitalmars.com/d/1.0/template.html#TemplateAliasParameter http://www.digitalmars.com/d/2.0/template.html#TemplateAliasParameter says: Template Alias Parameters TemplateAliasParameter: alias Identifier TemplateAliasParameterSpecializationopt TemplateAliasParameterDefaultopt TemplateAliasParameterSpecialization: : Type <---- ??? TemplateAliasParameterDefault: = Type <---- ??? What DOES work is this: //======================================= int defaultSymbol; int someParticularSymbol; //generic version for any someThing defaulting to defaultSymbol; class myAliasTemplate( alias someThing = defaultSymbol ) { //the generic (non-specialised case) } //specialised version for only someParticularSymbol; class myAliasTemplate( alias someThing: someParticularSymbol) { //the specialised case) } //======================================= Another approach: //======================================= class myAliasTemplate( alias someThing = defaultSymbol ) { static if( is(typeof(someThing)==int) ) { //Define contents for int (specialised) } else { //Define contents otherwise (generic) } } //======================================= I'm using DMD v1.036 on linux. Paul Thompson (p dot thompson at acfr dot usyd dot edu dot au )
Nov 02 2009