digitalmars.D - PLEASE EXPLAIN THE SPEC: alias type specialization
- Russell Lewis (19/19) Mar 11 2008 I have a set of templates which take alias parameters, and I want to
- Russell Lewis (7/7) Mar 11 2008 Just so you guys know, I made part of the title of my previous message
- BCS (3/13) Mar 11 2008 Ahh. What we all wouldn't give for a yank button... I think most of us h...
- Paul Thompson (45/64) Nov 02 2009 I was working through the grammar and found this too.
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
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
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
== Quote from Russell Lewis (webmaster villagersonline.com)'s articleI 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? RussI 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