Simple implementation of __FUNCTION

Rob T rob at ucora.com
Fri Nov 2 15:33:35 PDT 2012


On Friday, 2 November 2012 at 21:34:23 UTC, Philippe Sigaud wrote:
> By changing this to a standard function:
>
> const(char[]) __FUNCTION() @property
> {
>    return "__traits(identifier, __traits(parent, {}))";
> }
>
>
> ... the calling syntax is slightly easier on the eye:
>
> void main()
> {
>     writefln( "File: %s, Func: %s, Line: %d", __FILE__,
> mixin(__FUNCTION), __LINE__ );
>
>     //throw new Exception( "Error: Function " ~ 
> mixin(__FUNCTION) );
> }
>
> That is, mixin(__FUNCTION) instead of mixin(__FUNCTION!())
>
>
> Is there any downside to this?

That looks better. Not sure what the down side would be if any.

Unrelated to either form, I discovered it fails to compile when 
inside a function with "auto" as the return type.

auto test()
{
    throw new Exception(  mixin(__FUNCTION) );
    return 0;
}

Error: forward reference to test

but this works

int test()
{
    throw new Exception(  mixin(__FUNCTION) );
    return 0;
}

So we're kinda sunk for inclusion in phobos unless this error can 
be resolved.

I'll try the enum idea to see if that works.

--rt




More information about the Digitalmars-d mailing list