Accessing function frame from struct

Sebastien Alaiwan via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 25 06:01:33 PDT 2017


Hi guys,
here's my full code below.
My problem is that last "auto Y = X" assignment, that the 
compiler won't accept:

yo.globalFunction.DirectStruct.IndirectStruct.indirectMemberFunc 
cannot access frame of function yo.globalFunction

I was expecting X to be accessible from here.
Suprisingly, if I replace all "struct" with "class", the last 
assignment is accepted by the compiler.
Could someone please tell me why the scoping asymmetry between 
structs and classes?

void globalFunction()
{
   auto X = 0;

   struct DirectStruct
   {
     void directMemberFunc()
     {
       auto Y = X; // OK, X is accessible

       struct HybridStruct
       {
         void hybridFunc()
         {
           auto Y = X; // OK, X is accessible
         }
       }
     }

     struct IndirectStruct
     {
       void indirectMemberFunc()
       {
         auto Y = X; // Error: can't access frame of globalFunc
       }
     }
   }
}




More information about the Digitalmars-d-learn mailing list