Get calling this, if exists

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 23 20:16:58 PDT 2016


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");
}


More information about the Digitalmars-d-learn mailing list