January 30, 2006 Problem with mixins and overriding | ||||
---|---|---|---|---|
| ||||
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. |
January 31, 2006 Re: Problem with mixins and overriding | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Miller Attachments: | 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 |
Copyright © 1999-2021 by the D Language Foundation