Thread overview
__FUNCTION__?
Feb 25, 2013
js.mdnq
Feb 25, 2013
Vladimir Panteleev
Feb 25, 2013
Nick Treleaven
Feb 26, 2013
Andrej Mitrovic
Feb 26, 2013
Rob T
February 25, 2013
We have __FILE__ and __LINE__. Is there a __FUNCTION__ that gives the current function name? This helps with errors.
February 25, 2013
On Monday, 25 February 2013 at 16:32:50 UTC, js.mdnq wrote:
> We have __FILE__ and __LINE__. Is there a __FUNCTION__ that gives the current function name? This helps with errors.

Have a look here:

http://wiki.dlang.org/Using_string_mixins_for_logging
February 25, 2013
On 25/02/2013 16:32, js.mdnq wrote:
> We have __FILE__ and __LINE__. Is there a __FUNCTION__ that gives the
> current function name? This helps with errors.

You can define a string mixin to use instead of __FUNCTION__:

enum string parentName = q{__traits(identifier, __traits(parent, {}))};

void main()
{
    writeln(mixin(parentName)); // prints "main"
}

February 26, 2013
On 2/25/13, js.mdnq <js_adddot+mdng@gmail.com> wrote:
> We have __FILE__ and __LINE__. Is there a __FUNCTION__ that gives the current function name? This helps with errors.

We'll have it sometime soon.

http://d.puremagic.com/issues/show_bug.cgi?id=5140 https://github.com/D-Programming-Language/dmd/pull/1462
February 26, 2013
On Monday, 25 February 2013 at 16:32:50 UTC, js.mdnq wrote:
> We have __FILE__ and __LINE__. Is there a __FUNCTION__ that gives the current function name? This helps with errors.

As was previously stated there's a pending pull request that properly implements __FUNCTION__ and more.

If you can't wait for it, this is what I've been using ...

// put this in a module to re-use ...


// support functions
string fg_MakeFuncSig( string a_FName, string a_Sig )
{
	return a_FName  ~ "<" ~ a_Sig ~ ">";
}

string fg_MakeObjFuncSig( string a_OName, string a_FName, string a_Sig )
{
	return a_OName ~ "." ~ a_FName  ~ "<" ~ a_Sig ~ ">";
}

// function name only
enum __FUNCTION_NAME = q{ __traits(identifier, __traits(parent, {})) };

// function name and signature
enum __FUNCTION_SIG = q{ fg_MakeFuncSig( __traits(identifier, __traits(parent, {})), typeof(__traits(parent, {})).stringof ) };

// class or struct member function name and signature
enum __OBJ_FUNCTION_SIG = q{ fg_MakeObjFuncSig( typeof(this).stringof, __traits(identifier, __traits(parent, {})), typeof(__traits(parent, {})).stringof ) };

// example code showing use.

struct X
{
   void test( int a_i )
   {
      writeln( mixin(__OBJ_FUNCTION_SIG) );
   }

}

int main()
{
   writeln( mixin(__FUNCTION) );
   writeln( mixin(__FUNCTION_SIG) );
   X x;
   x.test(1);
}

--rt