Thread overview
Aliasing 'this' as a template parameter
Oct 19, 2006
Max Samuha
Oct 19, 2006
Max Samuha
Oct 20, 2006
Max Samuha
Oct 20, 2006
Lionello Lunesu
October 19, 2006
Walter, is it possible to make 'this' (and 'outer'?) aliasable in templates? It would be useful when mixing code into class declarations.

template TCode1()
{
	void wreck1()
	{
		char[] str = this.toString();
	}
}

template TCode2(alias Identifier)
{
	void wreck2(Identifier)
	{
		char[] str = Identifier.toString();
	}
}

class Foo
{
	mixin TCode1(); // works ok
	mixin TCode2(this); // complains that 'this' is not in a
non-static member

	void foo()
	{
		mixin TCode2!(this); // complains that there's no
match
	}
}

Btw, thank you who reply to my posts and get no reply back from me. It's not because i'm rude (well, i am in a way), i'm just still desparately trying to make sense out of the thing called D and haven't yet made enough progress for competent discussion.
October 19, 2006
On Thu, 19 Oct 2006 16:46:12 +0300, Max Samuha <maxter@i.com.ua> wrote:

>Walter, is it possible to make 'this' (and 'outer'?) aliasable in templates? It would be useful when mixing code into class declarations.
>
>template TCode1()
>{
>	void wreck1()
>	{
>		char[] str = this.toString();
>	}
>}
>
>template TCode2(alias Identifier)
>{
>	void wreck2(Identifier)
>	{
>		char[] str = Identifier.toString();
>	}
>}
>
>class Foo
>{
>	mixin TCode1(); // works ok
>	mixin TCode2(this); // complains that 'this' is not in a
>non-static member
>
>	void foo()
>	{
>		mixin TCode2!(this); // complains that there's no
>match
>	}
>}
>
>Btw, thank you who reply to my posts and get no reply back from me. It's not because i'm rude (well, i am in a way), i'm just still desparately trying to make sense out of the thing called D and haven't yet made enough progress for competent discussion.

Missing ! in mixin instantiations above
October 20, 2006
On Thu, 19 Oct 2006 21:00:10 +0300, Max Samuha <maxter@i.com.ua> wrote:

>On Thu, 19 Oct 2006 16:46:12 +0300, Max Samuha <maxter@i.com.ua> wrote:
>
>>Walter, is it possible to make 'this' (and 'outer'?) aliasable in templates? It would be useful when mixing code into class declarations.
>>
>>template TCode1()
>>{
>>	void wreck1()
>>	{
>>		char[] str = this.toString();
>>	}
>>}
>>
>>template TCode2(alias Identifier)
>>{
>>	void wreck2(Identifier)
>>	{
>>		char[] str = Identifier.toString();
>>	}
>>}
>>
>>class Foo
>>{
>>	mixin TCode1(); // works ok
>>	mixin TCode2(this); // complains that 'this' is not in a
>>non-static member
>>
>>	void foo()
>>	{
>>		mixin TCode2!(this); // complains that there's no
>>match
>>	}
>>}
>>
>>Btw, thank you who reply to my posts and get no reply back from me. It's not because i'm rude (well, i am in a way), i'm just still desparately trying to make sense out of the thing called D and haven't yet made enough progress for competent discussion.
>
>Missing ! in mixin instantiations above


It's nice to talk to myself and nobody seems to mind :) so i'm going to elaborate on the subject giving an example from a real project

I agree I made made quite a mess of mixins because i initially thought about them as a means to mix into a scope not only declarations but any arbitrary parametrized code, and still can't get used to the thought that it's not possible.

My problem: ASIO drivers for Windows are represented by com objects. The code style of the project requires that exceptions be thrown in exceptional situations while com objects use error codes for this. That is why i'm writing a wrapper converting errors to exceptions (actually it does more than that). in the log run, i found myself re-typing the following error checking code again and again:

class ASIODriver : IASIODriver
{
	...
	void someMethod()
	{
		ASIOError result = _co.someComMethod();

		if (result != ASIOError.OK && result !=
ASIOError.SUCCESS && ASIOException.enabled)
			throw new ASIOException(this /* this is the
driver 	wrapper */, result);
	}

}

ASIOException is defined like that:

ASIOException : Exception
{
	ASIOError error;
	IAsioDriver driver;

	this(IAsioDriver drv, ASIOError err)
	{
		char[] msg = drv.getErrorMessage();
		super(msg);

		driver = drv;
		...
	}
}

I could put the error checking code in a function but there is no guarantee that the function will always be inlined. Moreover, it creates one more frame in the exception's stack trace (the latter is not a big problem).

Then I naively thougnt that mixins would sort it out:

template TErrorChecker(alias Driver, alias Result)
{
	if (Result != ASIOError.OK && Result != ASIOError.SUCCESS &&
ASIOException.enabled)
			throw new ASIOException(Driver, Result); }

void someMethod()
{
	ASIO result = _co.SomeComMethod();
	mixin TErrorChecker!(this, result); // I agree that it's not
much less typing but looks cleaner. Sure, it does not compile
}

I see now that the only way is to be patient and practice typing or use function calls













October 20, 2006
Max Samuha wrote:
> It's nice to talk to myself and nobody seems to mind :) 

No responses doesn't mean nobody's reading ;)