www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - possible d error?

reply Charles Hixson <charleshixsn earthlink.net> writes:
enum doesn't seem to be working as defined in the 
documentation...unless I'm missing something pretty basic.
The code is:
enum   TokenTyp   {tPre, tPost, tConj};
TokenTyp[char[]]   tokenTyp;
void   test()
{  tokenTyp["if"]    =   tPre;
    tokenTyp["then"]  =   3;
    tokenTyp["not"]   =   tPre;
    tokenTyp["or"]    =   2;
    tokenTyp["else"]  =   2;
    tokenTyp["and"]   =   2;
}

The result is:
la1:~/projects/d/AIA/src2$ dmd -c errexample.d
errexample.d(4): undefined identifier tPre
errexample.d(4): cannot implicitly convert expression (tPre) of 
type int to TokenTyp
errexample.d(6): undefined identifier tPre
errexample.d(6): cannot implicitly convert expression (tPre) of 
type int to TokenTyp

Relevant examples from thedocumentation are:
enum X { A, B, C }	// named enum
enum { A, B = 5+7, C, D = 8, E }
and
enum X { A=3, B, C }

I tried moving the enum statement to within the body of the 
function, in case it was a name scoping problem, but the error 
message didn't change.
Jul 11 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
Try what I have below:

On Mon, 11 Jul 2005 14:22:13 -0700, Charles Hixson  
<charleshixsn earthlink.net> wrote:
 enum doesn't seem to be working as defined in the documentation...unless  
 I'm missing something pretty basic.
 The code is:
 enum   TokenTyp   {tPre, tPost, tConj};
 TokenTyp[char[]]   tokenTyp;
 void   test()
 {  tokenTyp["if"]    =   tPre;

tokenTyp["if"] = TokenTyp.tPre;
     tokenTyp["then"]  =   3;
     tokenTyp["not"]   =   tPre;

tokenTyp["not"] = TokenTyp.tPre;
     tokenTyp["or"]    =   2;
     tokenTyp["else"]  =   2;
     tokenTyp["and"]   =   2;
 }

Regan
 The result is:
 la1:~/projects/d/AIA/src2$ dmd -c errexample.d
 errexample.d(4): undefined identifier tPre
 errexample.d(4): cannot implicitly convert expression (tPre) of type int  
 to TokenTyp
 errexample.d(6): undefined identifier tPre
 errexample.d(6): cannot implicitly convert expression (tPre) of type int  
 to TokenTyp

 Relevant examples from thedocumentation are:
 enum X { A, B, C }	// named enum
 enum { A, B = 5+7, C, D = 8, E }
 and
 enum X { A=3, B, C }

 I tried moving the enum statement to within the body of the function, in  
 case it was a name scoping problem, but the error message didn't change.

Jul 11 2005
parent reply Charles Hixson <charleshixsn earthlink.net> writes:
That did it.  Thanks a lot!

So it was a name scoping problem, but not one that could be 
solved by localizing the declaration.  I still haven't been able 
to figure out when I need to use the type.name convention, though 
I now *usually* get it right, I still don't understand why, and 
when I run into problems, I don't understand them.  (Or, as this 
example showed, even recognize what's happening.)


Regan Heath wrote:
 Try what I have below:
 
 On Mon, 11 Jul 2005 14:22:13 -0700, Charles Hixson  
 <charleshixsn earthlink.net> wrote:
 enum doesn't seem to be working as defined in the 
 documentation...unless  I'm missing something pretty basic.
 The code is:
 enum   TokenTyp   {tPre, tPost, tConj};
 TokenTyp[char[]]   tokenTyp;
 void   test()
 {  tokenTyp["if"]    =   tPre;

tokenTyp["if"] = TokenTyp.tPre;
     tokenTyp["then"]  =   3;
     tokenTyp["not"]   =   tPre;

tokenTyp["not"] = TokenTyp.tPre;
     tokenTyp["or"]    =   2;
     tokenTyp["else"]  =   2;
     tokenTyp["and"]   =   2;
 }

Regan
 The result is:
 la1:~/projects/d/AIA/src2$ dmd -c errexample.d
 errexample.d(4): undefined identifier tPre
 errexample.d(4): cannot implicitly convert expression (tPre) of type 
 int  to TokenTyp
 errexample.d(6): undefined identifier tPre
 errexample.d(6): cannot implicitly convert expression (tPre) of type 
 int  to TokenTyp

 Relevant examples from thedocumentation are:
 enum X { A, B, C }    // named enum
 enum { A, B = 5+7, C, D = 8, E }
 and
 enum X { A=3, B, C }

 I tried moving the enum statement to within the body of the function, 
 in  case it was a name scoping problem, but the error message didn't 
 change.


Jul 11 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
Yes, a scoping issue.

On the enum page:
http://www.digitalmars.com/d/enum.html

It says:
"If the enum Identifier is present, the EnumMembers are declared in the  
scope of the enum Identifier."

Basically a named enum creates a new scope, an un-named one does not, eg.

enum { A,B,C };
enum Named { A,B,C };

void main()
{
   int i = A;
   int j = Named.A;
}

Regan

On Mon, 11 Jul 2005 14:40:06 -0700, Charles Hixson  
<charleshixsn earthlink.net> wrote:

 That did it.  Thanks a lot!

 So it was a name scoping problem, but not one that could be solved by  
 localizing the declaration.  I still haven't been able to figure out  
 when I need to use the type.name convention, though I now *usually* get  
 it right, I still don't understand why, and when I run into problems, I  
 don't understand them.  (Or, as this example showed, even recognize  
 what's happening.)


 Regan Heath wrote:
 Try what I have below:
  On Mon, 11 Jul 2005 14:22:13 -0700, Charles Hixson   
 <charleshixsn earthlink.net> wrote:
 enum doesn't seem to be working as defined in the  
 documentation...unless  I'm missing something pretty basic.
 The code is:
 enum   TokenTyp   {tPre, tPost, tConj};
 TokenTyp[char[]]   tokenTyp;
 void   test()
 {  tokenTyp["if"]    =   tPre;

     tokenTyp["then"]  =   3;
     tokenTyp["not"]   =   tPre;

     tokenTyp["or"]    =   2;
     tokenTyp["else"]  =   2;
     tokenTyp["and"]   =   2;
 }

 The result is:
 la1:~/projects/d/AIA/src2$ dmd -c errexample.d
 errexample.d(4): undefined identifier tPre
 errexample.d(4): cannot implicitly convert expression (tPre) of type  
 int  to TokenTyp
 errexample.d(6): undefined identifier tPre
 errexample.d(6): cannot implicitly convert expression (tPre) of type  
 int  to TokenTyp

 Relevant examples from thedocumentation are:
 enum X { A, B, C }    // named enum
 enum { A, B = 5+7, C, D = 8, E }
 and
 enum X { A=3, B, C }

 I tried moving the enum statement to within the body of the function,  
 in  case it was a name scoping problem, but the error message didn't  
 change.



Jul 11 2005