Either I'm just too stupid, or D is lacking something

Chris Nicholson-Sauls ibisbasenji at gmail.com
Tue Jul 25 12:25:50 PDT 2006


Wolfgang Draxinger wrote:
> I'd like to write e.g the following (note the '#' indicating,
> that the template parameter "name" is to be literally replaced
> by what is written at the respective point in the template
> instanciation. The rule would be, that what follows after a '#'
> in a template must be a valid identifier and not a statement or
> expression. The usage of such a parameter within a template
> declaration would automatically limit it's usage to mixins;
> implicit mixin expansion could be assumed, but I'd discourage
> that):
> 
> template GLTexParameter(T type, GLenum Param, #name)
> {
>         T #name(T value)
>         {
>                 static if ( is ( T == GLint ) ) {
>                         glTexParameteri(Target, Param, value);
>                 } else static if ( is ( T == GLfloat ) ) {
>                         glTexParameterf(Target, Param, value);
>                 }
>                 return value;
>         }
> 
>         T #name()
>         {
>                 T value;
>                 static if ( is ( T == GLint ) ) {
>                         glGetTexParameteriv(Target, Param, &value);
>                 } else static if ( is ( T == GLfloat ) ) {
>                         glGetTexParameterfv(Target, Param, &value);
>                 }
>                 return value;
>         }
> }
> 
> class Texture
> {
>         mixin GLTexParameter!(GLint, GL_TEXTURE_MIN_FILTER, min_filter);
>         mixin GLTexParameter!(GLint, GL_TEXTURE_MAG_FILTER, mag_filter);
>         mixin GLTexParameter!(GLfloat, GL_TEXTURE_PRIORITY, priority);
> /*...*/
> }
> 
> I used a '#' since it is not being assumed a token by the D specs
> right now. I'd also like '$' like in shell scripts or Perl, but
> that's a D token already, but right now I can't remember what it
> is for, or if it's actually used.

The '#' character does already have a meaning.  It is the prefix for "Special Token 
Sequences."  See the specs at:
http://www.digitalmars.com/d/lex.html#specialtokenseq

The '$' is currently used as an alias to the '.length' property of arrays, available only 
as a right-hand-side to the range sub-op in the slice-op.
array[0 .. $] == array[0 .. array.length]

Personally I don't think '$' having other uses would be a problem, as this usage is pretty 
distinct.  Also, while I'm not sure if '#' is the best choice for your proposal, I do 
agree that its something I'd love to see.  There have been a few times where I'd had to 
resort to tricks like the "named mixin + aliases" style, or ended up just copy-pasting 
whole sections of code and sighing a lot, especially if something changes.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list