Get calling this, if exists
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jun 24 08:35:57 PDT 2016
On 6/24/16 11:15 AM, Smoke Adams wrote:
> On Friday, 24 June 2016 at 03:16:58 UTC, Meta wrote:
>> On Friday, 24 June 2016 at 03:10:51 UTC, Mike Parker wrote:
>>> Oh, perhaps I misunderstood your question. Do you meant this:
>>>
>>> class Foo() {
>>> void bar() { Log(); } // Pass reference to Foo instance
>>> }
>>>
>>> void doSomething() { Log(); } // Null reference
>>>
>>> If so, the answer is no. And I don't see how that could work as a
>>> compile time parameter, given that the reference itself is a runtime
>>> value.
>>
>> It actually is possible. You just have to be explicit.
>>
>> void log(alias self)(string s)
>> {
>> pragma(msg, self.stringof);
>> }
>>
>> struct Test
>> {
>> void test(string s)
>> {
>> log!this(s);
>> }
>> }
>>
>> void main()
>> {
>> Test t;
>> t.test("asdf");
>> }
>
> I don't want to be explicit! One can be explicit with __FILE__ too but
> one doesn't have to be.
__FILE__ is a constant, so is __LINE__. __THIS__ would not be, so this
is somewhat different.
With UFCS, you can get close:
void log(T)(T obj, string s)
{
...
}
struct Test
{
void test(string s)
{
this.log(s);
}
}
But I believe you have to call with explict 'this'.
Perhaps an opDispatch can help, but seems an awful lot to avoid explicit
passing of this variable.
-Steve
More information about the Digitalmars-d-learn
mailing list