www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Problem with mixins and overriding

reply "Chris Miller" <chris dprogramming.com> writes:
template Mixer() // Mixin
{
	char[] foo()
	{
		writef("[Mixer.foo] ");
		return "mixer";
	}
}

class Foo // Base class
{
	char[] foo()
	{
		writef("[Foo.foo] ");
		return "foo";
	}
}

class FooBar: Foo
{
	mixin Mixer mixer;
	
	
	char[] foo()
	{
		writef("[FooBar.foo] ");
		//return "foobar";
		//return mixer.foo() ~ super.foo();
		return mixer.foo(); // Actually calling FooBar.foo() (itself) ?
	}
}

int main()
{
	writefln("Result = %s", (new FooBar).foo());
	return 0;
}


Instead of printing out "[Mixer.foo] Result = mixer" it gets stuck in an  
infinite loop printing "[FooBar.foo] " and then ends up causing a stack  
overflow. It looks like mixer.foo() is actually calling FooBar.foo(). I  
would assume "mixin Mixer mixer;" would override Foo's foo() and FooBar's  
foo() would override mixer's.
Jan 30 2006
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chris Miller schrieb am 2006-01-30:
 template Mixer() // Mixin
 {
 	char[] foo()
 	{
 		writef("[Mixer.foo] ");
 		return "mixer";
 	}
 }

 class Foo // Base class
 {
 	char[] foo()
 	{
 		writef("[Foo.foo] ");
 		return "foo";
 	}
 }

 class FooBar: Foo
 {
 	mixin Mixer mixer;
 	
 	
 	char[] foo()
 	{
 		writef("[FooBar.foo] ");
 		//return "foobar";
 		//return mixer.foo() ~ super.foo();
 		return mixer.foo(); // Actually calling FooBar.foo() (itself) ?
 	}
 }

 int main()
 {
 	writefln("Result = %s", (new FooBar).foo());
 	return 0;
 }


 Instead of printing out "[Mixer.foo] Result = mixer" it gets stuck in an  
 infinite loop printing "[FooBar.foo] " and then ends up causing a stack  
 overflow. It looks like mixer.foo() is actually calling FooBar.foo(). I  
 would assume "mixin Mixer mixer;" would override Foo's foo() and FooBar's  
 foo() would override mixer's.
Added to DStress as http://dstress.kuehne.cn/run/m/mixin_16_A.d http://dstress.kuehne.cn/run/m/mixin_16_B.d http://dstress.kuehne.cn/run/m/mixin_16_C.d http://dstress.kuehne.cn/run/m/mixin_16_D.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFD3zOq3w+/yD4P9tIRAipjAJ4+g51vm8zyO1DhY7A0EpfhkIkdDgCgtAKG In1LW6a3rn9tPNEGLAsyEaM= =dNhC -----END PGP SIGNATURE-----
Jan 31 2006