[D.typesystem] Suggestion for improving OO inheritance models
Andrej Mitrovic
andrej.mitrovich at gmail.com
Thu Sep 2 07:59:39 PDT 2010
Yeah, my mistake. I missread your post. :)
But, you can use mixin expressions to add overloaded methods. E.g.:
import std.stdio;
class Bar
{
void func()
{
writeln("Bar.func()");
}
mixin("void func(string x) { writeln(x); }");
}
void main()
{
Bar bar = new Bar;
bar.func();
bar.func("test");
}
So I'm wondering if there could be an easy way to use mixin
expressions with mixin templates..
On Thu, Sep 2, 2010 at 10:17 AM, Jacob Carlborg <doob at me.com> wrote:
> On 2010-09-01 23:39, Andrej Mitrovic wrote:
>>
>> You mean like this?:
>> module add_virtual_functions;
>>
>> import std.stdio : writeln;
>>
>> void main()
>> {
>> test();
>> }
>>
>> mixin template Foo()
>> {
>> void func()
>> {
>> writeln("Foo.func()");
>> }
>> }
>>
>> class Bar
>> {
>> void func()
>> {
>> writeln("Bar.func()");
>> }
>> }
>>
>> class Code : Bar
>> {
>> mixin Foo;
>> }
>>
>> void test()
>> {
>> Bar b = new Bar();
>> b.func(); // calls Bar.func()
>>
>> Code c = new Code();
>> c.func(); // calls Code.func()
>> }
>>
>
> No, that is overriding. Overloading is having several methods with the same
> name taking different number of parameters or parameters of different types.
>
> module test;
>
> mixin template Foo ()
> {
> void bar (int i) {};
> }
>
> class Bar
> {
> void bar () {};
> mixin Foo;
> }
>
> void main ()
> {
> auto bar = new Bar;
> bar.bar(4); // line 17
> bar.bar();
> }
>
> The above code results in these errors:
>
> test.d(17): Error: function test.Bar.bar () is not callable using argument
> types (int)
> test.d(17): Error: expected 0 arguments, not 1 for non-variadic function
> type void()
>
>
>> On Wed, Sep 1, 2010 at 11:30 PM, Jacob Carlborg<doob at me.com> wrote:
>>>
>>> On 2010-09-01 22:44, Philippe Sigaud wrote:
>>>>
>>>> On Wed, Sep 1, 2010 at 18:13, retard<re at tard.com.invalid> wrote:
>>>>
>>>>
>>>> Have you taken a loot at Scala& traits already? It would be a great
>>>> starting point.
>>>>
>>>>
>>>> Scala's traits are great! Implicits in Scala are quite interesting too.
>>>> Also, Haskell typeclasses
>>>>
>>>> I wonder if D can have part of Scala traits functionality with mixins?
>>>
>>> You can't use D template mixins to add methods that will overload
>>> existing
>>> methods.
>>>
>>>
>>> --
>>> /Jacob Carlborg
>>>
>
>
> --
> /Jacob Carlborg
>
More information about the Digitalmars-d
mailing list