Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 03, 2014 why it said no identifier for declarator … | ||||
---|---|---|---|---|
| ||||
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;} |
June 04, 2014 Re: why it said no identifier for declarator … | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | On Tuesday, 3 June 2014 at 22:10:06 UTC, bioinfornatics wrote:
> I do not see why it fail in debug output we see that tuple have a
> field with given name.
Your generated output (short and formatted)
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...
TL is a type.
TL variable;
variable.xStartsWith...
|
June 04, 2014 Re: why it said no identifier for declarator … | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On Wednesday, 4 June 2014 at 00:07:35 UTC, Jesse Phillips wrote:
> On Tuesday, 3 June 2014 at 22:10:06 UTC, bioinfornatics wrote:
>> I do not see why it fail in debug output we see that tuple have a
>> field with given name.
>
> Your generated output (short and formatted)
>
> 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...
>
> TL is a type.
>
> TL variable;
> variable.xStartsWith...
oh stupid me thanks Jesse sometime you do not see a problem visible as the nose on your face.
|
June 04, 2014 Re: why it said no identifier for declarator … | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On Wednesday, 4 June 2014 at 00:07:35 UTC, Jesse Phillips wrote: > On Tuesday, 3 June 2014 at 22:10:06 UTC, bioinfornatics wrote: >> I do not see why it fail in debug output we see that tuple have a >> field with given name. > > Your generated output (short and formatted) > > 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... > > TL is a type. > > TL variable; > variable.xStartsWith... after a tmeplate update -------------------------------- template toTuple(T){ static string maker(){ string assignFunction = "TL tl; "; 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() ); } -------------------------------- which generate this string: 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 tl; 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; This produce an error at mixin step: 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 '=' ; |
June 04, 2014 Re: why it said no identifier for declarator … | ||||
---|---|---|---|---|
| ||||
Posted in reply to bioinfornatics | I take it the output looks something like this: struct S { int a; } S s; s.a = 3; void main() { } Hope this clears up this next problem. Module scope doesn't get to utilize a variable, it can only initialize at compile-time. |
Copyright © 1999-2021 by the D Language Foundation