www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to get enclosing function as symbol ? (eg: __function__.stringof

reply Timothee Cour <thelastmammoth gmail.com> writes:
Is there any way to get the enclosing function as symbol ?

I'd like something like that:
alternative names would be:
__function__
__context__

----
auto fun(alias caller=__function__)(){
  //caller represents fun1!double
  return ReturnType!caller.init;
}

T fun1(T)(T x){
  assert(__function__.stringof==__FUNCTION__);
  alias fun=__function__;
  assert( is(ReturnType! __function__) == T);
  return fun();
}
void main(){fun1!double();}
----
Aug 17 2013
next sibling parent reply "JS" <js.mdnq gmail.com> writes:
On Sunday, 18 August 2013 at 01:52:50 UTC, Timothee Cour wrote:
 Is there any way to get the enclosing function as symbol ?

 I'd like something like that:
 alternative names would be:
 __function__
 __context__

 ----
 auto fun(alias caller=__function__)(){
   //caller represents fun1!double
   return ReturnType!caller.init;
 }

 T fun1(T)(T x){
   assert(__function__.stringof==__FUNCTION__);
   alias fun=__function__;
   assert( is(ReturnType! __function__) == T);
   return fun();
 }
 void main(){fun1!double();}
 ----
use a string mixin?
Aug 17 2013
parent "Nicolas Sicard" <dransic gmail.com> writes:
On Sunday, 18 August 2013 at 02:50:32 UTC, JS wrote:
 On Sunday, 18 August 2013 at 01:52:50 UTC, Timothee Cour wrote:
 Is there any way to get the enclosing function as symbol ?

 I'd like something like that:
 alternative names would be:
 __function__
 __context__

 ----
 auto fun(alias caller=__function__)(){
  //caller represents fun1!double
  return ReturnType!caller.init;
 }

 T fun1(T)(T x){
  assert(__function__.stringof==__FUNCTION__);
  alias fun=__function__;
  assert( is(ReturnType! __function__) == T);
  return fun();
 }
 void main(){fun1!double();}
 ----
use a string mixin?
I thought this would work but it doesn't: --- void foo(T)() { bar!__FUNCTION__(); } void bar(string Caller)() { mixin("alias caller = " ~ Caller ~ ";"); } void main() { foo!double(); } --- It works if foo isn't a template, though. The problem when foo is a template is that foo!double.foo seems to be an illegal construct for the compiler...
Aug 18 2013
prev sibling parent "Kapps" <opantm2+spam gmail.com> writes:
On Sunday, 18 August 2013 at 01:52:50 UTC, Timothee Cour wrote:
 Is there any way to get the enclosing function as symbol ?

 I'd like something like that:
 alternative names would be:
 __function__
 __context__

 ----
 auto fun(alias caller=__function__)(){
   //caller represents fun1!double
   return ReturnType!caller.init;
 }

 T fun1(T)(T x){
   assert(__function__.stringof==__FUNCTION__);
   alias fun=__function__;
   assert( is(ReturnType! __function__) == T);
   return fun();
 }
 void main(){fun1!double();}
 ----
I don't think you can pass it in to the function, but you can use __traits(parent) on a variable within the function to get the function.
Aug 17 2013