Thread overview
[Issue 2525] New: override of function from abstract base class's interface
Dec 19, 2008
d-bugmail
Mar 05, 2013
Maksim Zholudev
May 06, 2013
Andrej Mitrovic
May 07, 2013
Maksim Zholudev
Aug 07, 2013
Dicebot
Aug 07, 2013
Andrej Mitrovic
December 19, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2525

           Summary:  override of function from abstract base class's
                    interface
           Product: D
           Version: 1.038
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: diggory.hardy@gmail.com


interface I
{
        void foo();
}
abstract class A : I
{
}
class B : A
{
        override void foo () {}
}

The above code fails to compile:
overrideBaseInterface.d(10): function overrideBaseInterface.B.foo does not
override any function

It looks to me like B.foo is overriding I.foo and hence the override keyword should be valid.


-- 

March 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=2525



--- Comment #1 from Maksim Zholudev <maximzms@gmail.com> 2013-03-05 03:48:22 PST ---
DMD Git head: https://github.com/D-Programming-Language/dmd/commit/13b3bdbf3819fec810ebfb077957510612dfa815
--------------------
test.d(10): Error: function test.B.foo does not override any function, did you mean to override 'test.I.foo'?
--------------------
Yes, I did!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=2525


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-05-06 15:01:41 PDT ---
Technically you're not overriding, you're implementing. I think there is no bug here. I also think the following should be an error:

-----
interface I
{
    void foo();
}

class C : I
{
    override void foo () {}  // no error here currently
}
----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=2525



--- Comment #3 from Maksim Zholudev <maximzms@gmail.com> 2013-05-06 21:37:50 PDT ---
I think `override` keyword is useful to mark the methods that are supposed to be declared elsewhere.

In the following example I'm also (technically) implementing:
--------------------
abstract class A
{
    void foo();
}
class B : A
{
    void foo () {}
}
--------------------
test.d(7): Deprecation: overriding base class function without using override
attribute is deprecated (test.B.foo overrides test.A.foo)
--------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=2525


Dicebot <public@dicebot.lv> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |public@dicebot.lv


--- Comment #4 from Dicebot <public@dicebot.lv> 2013-08-07 12:38:20 PDT ---
(In reply to comment #2)
> Technically you're not overriding, you're implementing. I think there is no bug here. I also think the following should be an error:
> 
> -----
> interface I
> {
>     void foo();
> }
> 
> class C : I
> {
>     override void foo () {}  // no error here currently
> }
> ----

Voting up this bug report.

Semantical difference between overriding and implementing is very subtle here. One can also say that it overrides function with empty implementation.

However, what does matter here is pragmatical usability it brings to the table.

http://dlang.org/attribute.html#override
> The override attribute applies to virtual functions. It means that the function must override a function with the same name and parameters in a base class. The override attribute is useful for catching errors when a base class's member function gets its parameters changed, and all derived classes need to have their overriding functions updated.

This description does miss one important use case - verifying that you do not introduce a new function symbol in the class hierarchy. Consider this example: in the process of wild refactoring interface signature changes and dependent classes get modified. A sloppy programmer works on new implementation of that functions and forgets that there is already existing one. However, code compiles and works, polluting sources with unused function body.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=2525



--- Comment #5 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-08-07 13:17:16 PDT ---
(In reply to comment #4)
> A sloppy programmer works on new implementation of that
> functions and forgets that there is already existing one. However, code
> compiles and works, polluting sources with unused function body.

I agree 'override' adds to the readability, however D already protects you against function hiding and function hijacking. For example:

-----
class A
{
    void foo() { }
}

class B : A
{
    void foo() { }
}
-----

test.d(10): Deprecation: overriding base class function without using override
attribute is deprecated (test.B.foo overrides test.A.foo)

I don't know why I wrote http://d.puremagic.com/issues/show_bug.cgi?id=2525#c2, I do believe override makes the code more readable. Ideally we would have an "implements" keyword, but you know, keyboard bloat and all that.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------