Comment #0 by tim.matthews7 — 2009-06-29T21:18:55Z
This code compiles:
module test;
enum A
{
a = 0
}
enum B
{
b = 1
}
class BaseClass
{
final A getEnum()
{
return A.a;
}
}
class DerivedClass : BaseClass
{
B getEnum()
{
return B.b;
}
}
void main()
{
auto der = new DerivedClass();
der.getEnum();
}
Comment #1 by yebblies — 2011-06-16T04:23:17Z
B getEnum()
is not overriding
final A getEnum()
It's simply creating a new function.
Yet another reason the override attribute should be compulsory.
Comment #2 by code — 2011-06-16T05:11:37Z
Reopening this, as the compiler doesn't even error out with the »-w« switch – the »override compulsory« check seems to be broken in the presence of »final«.
Comment #3 by yebblies — 2011-06-16T05:18:11Z
(In reply to comment #2)
> Reopening this, as the compiler doesn't even error out with the »-w« switch –
> the »override compulsory« check seems to be broken in the presence of »final«.
Where should the override attribute be required? There is no overriding happening anywhere in the example.
Comment #4 by code — 2011-06-16T05:23:12Z
R(In reply to comment #3)
> (In reply to comment #2)
> > Reopening this, as the compiler doesn't even error out with the »-w« switch –
> > the »override compulsory« check seems to be broken in the presence of »final«.
>
> Where should the override attribute be required? There is no overriding
> happening anywhere in the example.
Remove the »final« attribute and compile the example with -w, and you'll see.
Besides, since when would it be allowed to have two public methods with the same signature?
Comment #5 by code — 2011-06-16T05:23:13Z
R(In reply to comment #3)
> (In reply to comment #2)
> > Reopening this, as the compiler doesn't even error out with the »-w« switch –
> > the »override compulsory« check seems to be broken in the presence of »final«.
>
> Where should the override attribute be required? There is no overriding
> happening anywhere in the example.
Remove the »final« attribute and compile the example with -w, and you'll see.
Besides, since when would it be allowed to have two public methods with the same signature?
Comment #6 by bearophile_hugs — 2011-06-16T05:25:54Z
Even if D is working according to specs, I think this is bug-prone enough to deserve some warning or even an error.
Comment #7 by yebblies — 2011-06-16T06:48:41Z
(In reply to comment #5)
>
> Remove the »final« attribute and compile the example with -w, and you'll see.
>
I get an error when compiling without the final attribute, -w or not.
testx.d(21): Error: function testx.DerivedClass.getEnum of type B() overrides but is not covariant with testx.BaseClass.getEnum of type A()
> Besides, since when would it be allowed to have two public methods with the
> same signature?
Yes, overloading on return type is disabled for functions in the same scope, but the rules are a little less strict for deriving classes...
Looking at the spec, it seems to be explicitly disallowed for functions that would otherwise be virtual.
It can be very useful with template functions, as they need to be redefined in each class, and is allowed for static functions.
I might be misunderstanding, please post a test case that shows what you mean.
(In reply to comment #6)
> Even if D is working according to specs, I think this is bug-prone enough to
> deserve some warning or even an error.
I think it would be much clearer if override was mandatory, and I'm fairly sure there's already a report for that. If there's something else you'd like, please, write it up. A test case that compiles does not constitute an enhancement request without additional information. (unless it's very, very obvious)
Comment #8 by schveiguy — 2011-06-16T07:54:25Z
I think it is a bug. If the case was that a final function could be masked by a derived class, then this should compile:
module test;
enum A
{
a = 0,
b = 1
}
class BaseClass
{
final A getEnum()
{
return A.a;
}
}
class DerivedClass : BaseClass
{
A getEnum()
{
return A.b;
}
}
But I get the error:
bug3113.d(19): Error: function test.DerivedClass.getEnum cannot override final function test.BaseClass.getEnum
This occurs even when I mark DerivedClass' function as final.
I was about to mark this as invalid, citing this as a better example, but since the compiler complains about masking in this case, I don't see why the original case is allowed. I believe this kind of thing is allowed in C++.
Comment #9 by code — 2011-06-16T08:06:26Z
(In reply to comment #8)
> This occurs even when I mark DerivedClass' function as final.
I think it is quite clear that the example you gave shouldn't compile, as the spec has: »Functions marked as final may not be overridden in a derived class, unless they are also private.«
The question now is whether the same behavior should also apply to the example from above. I'm strongly in favor of that, because otherwise, there can be situation where the following two pieces of code don't refer to the same »bar()«, which is completely contrary to how classes usually work in D:
---
auto foo = new Derived();
foo.bar();
---
---
Base foo = new Derived();
foo.bar();
---
Comment #10 by yebblies — 2011-06-16T08:09:46Z
(In reply to comment #9)
> (In reply to comment #8)
> > This occurs even when I mark DerivedClass' function as final.
>
> I think it is quite clear that the example you gave shouldn't compile, as the
> spec has: »Functions marked as final may not be overridden in a derived class,
> unless they are also private.«
>
> The question now is whether the same behavior should also apply to the example
> from above. I'm strongly in favor of that, because otherwise, there can be
> situation where the following two pieces of code don't refer to the same
> »bar()«, which is completely contrary to how classes usually work in D:
>
Would you also apply this rule to static and template functions?
Comment #11 by schveiguy — 2011-06-16T08:20:45Z
> (In reply to comment #9)
> > (In reply to comment #8)
> > > This occurs even when I mark DerivedClass' function as final.
> >
> > I think it is quite clear that the example you gave shouldn't compile, as the
> > spec has: »Functions marked as final may not be overridden in a derived class,
> > unless they are also private.«
> >
> > The question now is whether the same behavior should also apply to the example
> > from above.
I don't think this is really overriding. You can still call the base function. It's more like masking. But the compiler is definitely behaving inconsistently, so it's still a bug, either way.
> > I'm strongly in favor of that, because otherwise, there can be
> > situation where the following two pieces of code don't refer to the same
> > »bar()«, which is completely contrary to how classes usually work in D:
> >
> Would you also apply this rule to static and template functions?
I would actually recommend to nix that part of the language, just allow masking of final functions as long as they are not marked override. I.e. final stops the virtual chain, and any derived class will not be able to override the base virtual function. However, it can start up a new chain by *not* specifying override. I think that requiring override is essential to doing something like this.
Comment #12 by robert.schadek — 2024-12-13T17:50:26Z