why it said no identifier for declarator …

bioinfornatics via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 3 15:10:06 PDT 2014


Hi,

I have this code http://ur1.ca/hg3un

pragma (to debug) line 61 said:

alias TL = Tuple!(int,"x",  bool function( const ref string ),
"xStartsWith ", bool function( const ref string ), "xEndsWith ",
int,"y",  bool function( const ref string ), "yStartsWith ", bool
function( const ref string ), "yEndsWith ") ;TL.xStartsWith =
__traits(getAttributes, T.x)[0].startsWith; TL.xEndsWith   =
__traits(getAttributes, T.x)[0].endsWith; TL.yStartsWith =
__traits(getAttributes, T.y)[0].startsWith; TL.yEndsWith   =
__traits(getAttributes, T.y)[0].endsWith; alias toTuple = TL ;


and compiler when it try to run mixin line 62 raise this error:

attribute.d-mixin-62(62): Error: no identifier for declarator
TL.xStartsWith
attribute.d-mixin-62(62): Error: Declaration expected, not '='
attribute.d-mixin-62(62): Error: no identifier for declarator
TL.xEndsWith
attribute.d-mixin-62(62): Error: Declaration expected, not '='
attribute.d-mixin-62(62): Error: no identifier for declarator
TL.yStartsWith
attribute.d-mixin-62(62): Error: Declaration expected, not '='
attribute.d-mixin-62(62): Error: no identifier for declarator
TL.yEndsWith
attribute.d-mixin-62(62): Error: Declaration expected, not '='
attribute.d(73): Error: template instance
incunable.attribute.toTuple!(Data) error instantiating


I do not see why it fail in debug output we see that tuple have a
field with given name.


thanks



code if you do not want open another tab

---------- code -----------
module incunable.attribute;
import std.stdio;
import std.typecons     : Tuple;
import std.typetuple    : Filter;

struct section
{
      public bool function( const ref string ) startsWith;
      public bool function( const ref string ) endsWith;

      public this(  bool function( const ref string ) pred1,  bool
function( const ref string ) pred2 )
      {
          startsWith  = pred1;
          endsWith    = pred2;
      }
}

template hasSection(T)
{
      static bool helper()
      {
          foreach(memberName; __traits(allMembers, T))
          {
              foreach(attr; __traits(getAttributes,
__traits(getMember, T, memberName)))
              {
                  static if( isSection!(attr) )
                      return true;
              }
          }
          return false;
      }

      enum hasSection = helper();
}


template isSection(alias attr)
{
      enum isSection = is( typeof(attr) == section );
}


template toTuple(T){
      static string maker(){
          string assignFunction = "";
          string statement      = "alias TL = Tuple!(";
          foreach(const memberName; __traits(allMembers, T)){
              mixin(`alias f = Filter!(isSection,
__traits(getAttributes, T.` ~ memberName ~ `));`);
              if( f.length == 1 )
              {
                  statement   ~= typeof(__traits(getMember, T,
memberName)).stringof  ~ ",\"" ~ memberName ~ "\", "
                              ~ " bool function( const ref string
), \"" ~ memberName ~ "StartsWith \","
                              ~ " bool function( const ref string
), \"" ~ memberName ~ "EndsWith " ~ "\", " ;
                  assignFunction ~= "TL." ~ memberName ~
"StartsWith = __traits(getAttributes, T." ~ memberName ~
")[0].startsWith; "
                                 ~  "TL." ~ memberName ~ "EndsWith
   = __traits(getAttributes, T." ~ memberName ~ ")[0].endsWith; ";
              }
          }
          statement = statement[0..$-2] ~ ") ;" ; // $-2 to remove
extra comma
          return statement~assignFunction~ "alias toTuple = TL ;";
      }
      pragma( msg, maker() );
      mixin( maker() );
}

struct Data
{
      @section( (const ref string a) => a == "42", (const ref
string a) => a == "8" )
      int x;
      @section( (const ref string a) => a == "8", (const ref string
a) => a == "42" )
      int y;
}

void main(){alias a =toTuple!Data;}


More information about the Digitalmars-d-learn mailing list