www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - accessing your scope for template metaprogramming ?

reply Sean Cavanaugh <WorksOnMyMachine gmail.com> writes:
Is there any way to access the your current scope?  For instance the 
following pseudocode is what I am after more or less:


class Foo
{
   alias byte function(int) BarCompatibleInterface;

   byte Bar(int)
   {
     static assert(is(typeof(localscope) == function));

     static assert(is(std.traits.ReturnType!(localscope) == 
std.traits.ReturnType!(BarCompatibleInterface));

     static assert(is(std.traits.ParameterTypeTuple!(localscope) == 
std.traits.ParameterTypeTuple!(BarCompatibleInterface));

     static assert(is(typeof(localscope.outer) == class));

     static assert(is(typeof(localscope.outer.outer) == module));
   }
}


My desire for this is to help the compiler generate better error 
messages from compilation of string mixins, as they could access the 
function and/or class they are in and give a better error message from 
within a static assert() construct.
Apr 16 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Sean Cavanaugh:

 Is there any way to access the your current scope?  For instance the 
 following pseudocode is what I am after more or less:
 
 
 class Foo
 {
    alias byte function(int) BarCompatibleInterface;
 
    byte Bar(int)
    {
      static assert(is(typeof(localscope) == function));

You have just invented another purpose for something like a _function_, that refers to the current function that I have originally desired to solve this different problem. See my Comments 1 and 2 here: http://d.puremagic.com/issues/show_bug.cgi?id=5140 Bye, bearophile
Apr 16 2011