Thread overview
Bug? partially implemented interfaces
Jan 01, 2004
Hauke Duden
Jan 02, 2004
J C Calvarese
Jan 03, 2004
Hauke Duden
January 01, 2004
This may be a compiler bug, or maybe I'm just doing something wrong. The following code does not compile:


interface I
{
	void a();
	void b();
}

class Impl : I
{
	void a()
	{
	}
}

The compiler produces the following error:

"class Impl interface function I.b is not implemented."

I want "Impl" to be an abstract class that implements default behaviour for some of the interface functions, but leaves other interface functions unimplemented.

I have tried adding the line "abstract void b();" to the class definition, but that doesn't change anything. Using "abstract class" instead of "class" also has no effect on this error.

There doesn't seem to be anything in the docs about how abstract classes are done in D, except for the fact that "abstract" is mentioned in the keyword list.

So, how can I partially implement an interface in an abstract class?


Hauke


January 02, 2004
Hauke Duden wrote:
> This may be a compiler bug, or maybe I'm just doing something wrong. The following code does not compile:
> 
> 
> interface I
> {
>     void a();
>     void b();
> }
> 
> class Impl : I
> {
>     void a()
>     {
>     }
> }
> 
> The compiler produces the following error:
> 
> "class Impl interface function I.b is not implemented."
> 
> I want "Impl" to be an abstract class that implements default behaviour for some of the interface functions, but leaves other interface functions unimplemented.
> 
> I have tried adding the line "abstract void b();" to the class definition, but that doesn't change anything. Using "abstract class" instead of "class" also has no effect on this error.
> 
> There doesn't seem to be anything in the docs about how abstract classes are done in D, except for the fact that "abstract" is mentioned in the keyword list.
> 
> So, how can I partially implement an interface in an abstract class?
> 
> 
> Hauke
> 
> 

I don't know why your code is illegal, but the following does compile:


interface I
{
    void a();
    void b();
}

class Impl : I
{
    void a()
    {
    }

    void b()
    {
    }

}

class Kid : Impl
{
    void b()
    {
        printf("overriden method\n\0");
    }

}

void main()
{
    Kid k = new Kid();
    k.b();
}


If you want to remind yourself to override the method, you could add an "assert(0);" to the method that you want to abstract.

I'm not really sure what your goal is (all this abstract stuff is over my head), but I hope this might help.


-- 
Justin
http://jcc_7.tripod.com/d/
January 03, 2004
J C Calvarese wrote:
> I don't know why your code is illegal, but the following does compile:
> 
> 
> interface I
> {
>     void a();
>     void b();
> }
> 
> class Impl : I
> {
>     void a()
>     {
>     }
> 
>     void b()
>     {
>     }
> 
> }

<snip>

> If you want to remind yourself to override the method, you could add an "assert(0);" to the method that you want to abstract.
> 
> I'm not really sure what your goal is (all this abstract stuff is over my head), but I hope this might help.

What I want to do is avoid adding a dummy implementation for b. A dummy function with an assert doesn't quite cut it, since then the class could be instantiated, even though it is supposed to be abstract. Not to mention that this would also be a very dirty hack AND it would make another compile-time condition a runtime condition.

D supports abstract classes (right?), so a class that only partially implements an interface should simply be abstract. I just don't know how to get the compiler to recognize that.

Hauke