Thread overview
Non-Virtual Interfaces
March 04, 2011
I'm trying to use NVI idiom but i keep getting errors from dmd.

This is my setup:

module test;

import std.stdio;

interface IBase {
	void foo();
	void bar();
}

interface IBar : IBase {
	final void bar() {
		writefln("IBar.bar()");
	}
}

class Foo : IBar {

	void foo() {
		writefln("Foo.foo()");
	}
}

void main() {

	Foo foo = new Foo();
	foo.foo();
}

When I try to compile it i get "test.d(16): Error: class test.Foo interface function IBar.bar isn't implemented"

And if I try to define bar() in Foo i receive "test.d(22): Error:
function test.Foo.bar cannot override final function
IBar.test.IBar.bar"
which is expected since IBar.bar() is final.

So, am I missing some point about NVIs here or is it just not yet implemented in dmd?
March 04, 2011
On Fri, 04 Mar 2011 05:17:00 -0500, Aleksandar Ružičić <ruzicic.aleksandar@gmail.com> wrote:

> I'm trying to use NVI idiom but i keep getting errors from dmd.
>
> This is my setup:
>
> module test;
>
> import std.stdio;
>
> interface IBase {
> 	void foo();
> 	void bar();
> }
>
> interface IBar : IBase {
> 	final void bar() {
> 		writefln("IBar.bar()");
> 	}
> }
>
> class Foo : IBar {
>
> 	void foo() {
> 		writefln("Foo.foo()");
> 	}
> }
>
> void main() {
>
> 	Foo foo = new Foo();
> 	foo.foo();
> }
>
> When I try to compile it i get "test.d(16): Error: class test.Foo
> interface function IBar.bar isn't implemented"
>
> And if I try to define bar() in Foo i receive "test.d(22): Error:
> function test.Foo.bar cannot override final function
> IBar.test.IBar.bar"
> which is expected since IBar.bar() is final.
>
> So, am I missing some point about NVIs here or is it just not yet
> implemented in dmd?

The traditional explanation of NVI is that the final function is never virtual.  In your case, bar must be virtual at the IBase level, so it must go in the vtable.

I'm unsure whether this is intended to be a bug or a feature.

What you may want to consider is an abstract class instead of NVI, as long as you don't need multiple inheritance, it should be fine.

-Steve
March 04, 2011
>
> What you may want to consider is an abstract class instead of NVI, as long as you don't need multiple inheritance, it should be fine.
>
> -Steve
>

Well, I've decided to give NVI a try just because multiple inheritance would be best way to do what I want (aldo I hate that feature of C++ and just don't use it) but it seems I can't do it with a NVI either... So back to the drawing board for me :)

thanks for reply

- Aleksandar