__FUNCTION__?

Rob T alanb at ucora.com
Tue Feb 26 15:36:11 PST 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


More information about the Digitalmars-d-learn mailing list