www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Non-Virtual Interface and Interface Implementation

reply Matthias Frei <matfrei ethz.ch> writes:
Hi,

i had the seemingly innocent idea to use the "NVI idiom" in the 
following way:

interface Foo {
	void foo();
}

interface FooFoo : Foo {
	final void foo() {
		// do something with bar()
	}
	void bar();
}

class Bar : FooFoo {
	void bar() {
		// do something
	}
}

My idea was to check some default cases etc. in the FooFoo.foo() and 
call the bar() implementation from there.

However it turned out that is is not possible:
  Error: class test.Bar interface function FooFoo.foo isn't implemented

This is very weird, because of course Bar cannot implement foo() as it 
is declared final in FooFoo.

Is there some particular reason that this does not work?

Matthias
Nov 11 2011
next sibling parent reply Trass3r <un known.com> writes:
Am 11.11.2011, 17:16 Uhr, schrieb Matthias Frei <matfrei ethz.ch>:

 Hi,

 i had the seemingly innocent idea to use the "NVI idiom" in the  
 following way:

 interface Foo {
 	void foo();
 }

 interface FooFoo : Foo {
 	final void foo() {
 		// do something with bar()
 	}
 	void bar();
 }

 class Bar : FooFoo {
 	void bar() {
 		// do something
 	}
 }

 My idea was to check some default cases etc. in the FooFoo.foo() and  
 call the bar() implementation from there.

 However it turned out that is is not possible:
   Error: class test.Bar interface function FooFoo.foo isn't implemented

 This is very weird, because of course Bar cannot implement foo() as it  
 is declared final in FooFoo.

 Is there some particular reason that this does not work?

 Matthias

Probably a bug. final interface methods are relatively new, I guess nobody has tested interfaces inheriting interfaces yet. It also doesn't error on bar missing override.
Nov 11 2011
next sibling parent "Marco Leise" <Marco.Leise gmx.de> writes:
Am 11.11.2011, 17:38 Uhr, schrieb Trass3r <un known.com>:

 Am 11.11.2011, 17:16 Uhr, schrieb Matthias Frei <matfrei ethz.ch>:

 Hi,

 i had the seemingly innocent idea to use the "NVI idiom" in the  
 following way:

 interface Foo {
 	void foo();
 }

 interface FooFoo : Foo {
 	final void foo() {
 		// do something with bar()
 	}
 	void bar();
 }

 class Bar : FooFoo {
 	void bar() {
 		// do something
 	}
 }

 My idea was to check some default cases etc. in the FooFoo.foo() and  
 call the bar() implementation from there.

 However it turned out that is is not possible:
   Error: class test.Bar interface function FooFoo.foo isn't implemented

 This is very weird, because of course Bar cannot implement foo() as it  
 is declared final in FooFoo.

 Is there some particular reason that this does not work?

 Matthias

Probably a bug. final interface methods are relatively new, I guess nobody has tested interfaces inheriting interfaces yet. It also doesn't error on bar missing override.

Wow, this works? I was just recently thinking about it. I don't think many languages allow implementations in interfaces and I appreciate it.
Nov 11 2011
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, November 11, 2011 10:28 Andrej Mitrovic wrote:
 I thought interfaces can't have function bodies?

They can for final functions (which is unique to D, I believe). There are a few things about interfaces that are unique to D, but I'd have to go reread the relevant section in TDPL to remember all of the details. - Jonathan M Davis
Nov 11 2011
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 11/11/2011 07:28 PM, Andrej Mitrovic wrote:
 I thought interfaces can't have function bodies?

They can have final functions with bodies in it, Andrei calls it the 'Non-Virtual Interface Idiom'. It is described in TDPL starting from page 213.
Nov 11 2011
prev sibling parent Matthias Frei <matfrei ethz.ch> writes:
On 11/11/2011 05:38 PM, Trass3r wrote:
 Am 11.11.2011, 17:16 Uhr, schrieb Matthias Frei <matfrei ethz.ch>:

 Hi,

 i had the seemingly innocent idea to use the "NVI idiom" in the
 following way:

 interface Foo {
 void foo();
 }

 interface FooFoo : Foo {
 final void foo() {
 // do something with bar()
 }
 void bar();
 }

 class Bar : FooFoo {
 void bar() {
 // do something
 }
 }

 My idea was to check some default cases etc. in the FooFoo.foo() and
 call the bar() implementation from there.

 However it turned out that is is not possible:
 Error: class test.Bar interface function FooFoo.foo isn't implemented

 This is very weird, because of course Bar cannot implement foo() as it
 is declared final in FooFoo.

 Is there some particular reason that this does not work?

 Matthias

Probably a bug. final interface methods are relatively new, I guess nobody has tested interfaces inheriting interfaces yet. It also doesn't error on bar missing override.

Ok, thanks. Still cool that it should work ;)
Nov 13 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I thought interfaces can't have function bodies?
Nov 11 2011