Get calling this, if exists

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 24 10:51:32 PDT 2016


On 6/24/16 1:19 PM, Smoke Adams wrote:
>
> The problem with UFCS is that I am using variadic parameters. The
> logging function isn't designed to accept the first parameter as this.

Then you would need to change it?

> It would be much easier to simply have the compiler "insert" it using
> __THIS__. Just because it isn't constant isn't really that big of a deal
> to the compiler. It could just manually add the parameter at the end of
> of the arguments.

Yes, easier for you not having to do anything. It's only the compiler 
writers who have to figure out how to add this feature for you :)

> I would also have to convert all the calls to this.log.
>
> This becomes problematic because not not all calls are inside objects.

Possibly you can use overloads. In any case, I think there is some work 
you need to do to get what you want.

> We have __FUNCTION__ so, __THIS__ seems somewhat natural. Null for
> outside of objects is fine.

__FUNCTION__ is still a compile-time constant.

Have you considered a mixin or opDispatch to help you out? (untested)

mixin template addLog()
{
    log(this This, T...)(T args)
    {
       return .log(cast(This)this, args);
    }
}

class MyBaseObj
{
    mixin addLog;
}

class MyObj : MyBaseObj
{
    void foo()
    {
       log(1, 2, 3); // calls .log(cast(MyObj)this, 1, 2, 3)
    }
}

-Steve


More information about the Digitalmars-d-learn mailing list