> On Mon, 28 Apr 2008 10:11:23 -0300, Ary Borenszweig
A class can either be abstract or not abstract. Currently in D, if you
don't mark a class as abstract, it can still be it if it contains an
abstract method:
class Foo {
abstract void someAbstract();
void nonAbstract() {
}
}
When designing a class, you have in mind whether the class is going to
be abstract or not. If it's not going to be abstract, you want the
compiler to help you by telling you "You made a mistake. This class is
still abstract because you didn't implement method foo".
So I want to extend Foo with a class Bar, but I want Bar to be not abstract.
class Bar : Foo {
}
I compile, and it gives no error, of course. But I want there to be an
error there. The only way I can get an error is by making a dummy
function that instantiates Bar:
void blah() {
Bar bar = new Bar();
}
main.d(14): Error: cannot create instance of abstract class Bar
main.d(14): Error: function someAbstract is abstract
The problems with this approach are two:
- You have to make a dummy function to check whether you implemented
Bar correctly.
- You get two errors for each instantiation of Bar, if it's abstract
(ugly).
Why not make "abstract" mandatory for a class if it's intended to be
abstract, and the absence of "abstract" to mean "not abstract"? Java
works this way, and I think it is for the reasons I mentioned.
Another advantage is that just by seeing the start of class definition
you can tell whether a class is abstract or not. You don't have to see
if any method is marked as abstract, or go to the superclasses to see if
there is a method that is still not implemented.
(also, it would be nice if the compiler could tell you all the methods
that still need an implementation, rather than just one)
Comment #1 by gide — 2009-05-06T10:17:41Z
Another example.
main.d
------
class Foo1 {
abstract void foo();
}
class Bar1 : Foo1 {
}
abstract class Foo2 {
abstract void foo();
}
class Bar2 : Foo2 {
}
void main () {
debug {
auto f1 = new Bar1;
auto f2 = new Bar2;
}
}
C:> dmd -w -debug main.d
main.d(17): Error: cannot create instance of abstract class Bar1
main.d(17): Error: function foo is abstract
main.d(18): Error: cannot create instance of abstract class Bar2
main.d(18): Error: function foo is abstract
Should not compile or at least give a warning, but does neither.
C:> dmd -w main.d
Comment #2 by smjg — 2009-05-06T16:42:41Z
http://www.digitalmars.com/d/2.0/attribute.html#abstract (and similarly 1.0)
"Classes become abstract if they are defined within an abstract attribute, or if any of the virtual member functions within it are declared as abstract."
The compiler is behaving correctly, therefore this is an enhancement request.
Comment #3 by gide — 2010-07-27T07:17:03Z
*** Issue 4512 has been marked as a duplicate of this issue. ***
Comment #4 by bearophile_hugs — 2010-10-29T05:01:02Z
(In reply to comment #2)
> The compiler is behaving correctly, therefore this is an enhancement request.
We need a different term to tell apart true enhancement requests (where someone asks for a new feature or new subfeature) from bugs in the specs.
This is a case where the specs are suboptimal, so this is not a true enhancement request, it's a way to fix a little mistake in the D specs.
Comment #5 by smjg — 2010-10-30T05:57:20Z
(In reply to comment #4)
> This is a case where the specs are suboptimal, so this is not a true
> enhancement request, it's a way to fix a little mistake in the D specs.
Have you evidence that this is a mistake, i.e. not what Walter intended to write?
Comment #6 by bearophile_hugs — 2010-10-30T14:56:35Z
(In reply to comment #5)
> Have you evidence that this is a mistake, i.e. not what Walter intended to
> write?
I have no evidence, but regardless the origin of this current situation, the Java design is the correct one (unless someone shows me otherwise) and I think here it's better to modify D specs & implementation. And I think it's better to do this change as soon as possible, before lot of D2 code is written, to reduce troubles of fixing code later.
Comment #7 by smjg — 2010-10-30T17:22:30Z
Certainly the Java design is more sensible on this. But "correct" in terms of design is to some degree a matter of opinion. There are a number of aspects of D's design that I would consider mistakes - inheritance protection, archaic switch syntax, implicit narrowing conversions just to name a few. There are probably lots filed here. How would we label such issues on this basis, anyway?
Comment #8 by andrej.mitrovich — 2013-01-20T12:26:13Z
I don't think this will fly. Abstract on the class name itself makes it non-instantiable, however subclasses are not abstract and can be instantiated:
// can't be instantiated
abstract class Foo { void foo() { } }
// is not abstract and can be instantiated
class Bar : Foo { }
Whether or not there's an abstract keyword next to the class name doesn't tell you whether the class actually has abstract methods, so there's no benefit forcing you to add 'abstract' to the name.
Comment #9 by smjg — 2013-01-21T05:27:53Z
(In reply to comment #8)
> I don't think this will fly.
It's up to Walter whether this will be implemented.
> Abstract on the class name itself makes it non-instantiable,
> however subclasses are not abstract and can be instantiated:
It isn't clear to me what relevance this has. The point of this request is to require abstract classes to be declared as abstract. You're talking about classes that already are declared as abstract.
> Whether or not there's an abstract keyword next to the class name
> doesn't tell you whether the class actually has abstract methods,
> so there's no benefit forcing you to add 'abstract' to the name.
It isn't supposed to tell you whether it has abstract methods. It's supposed to tell you whether the class is abstract, i.e. barred from direct instantiation.
With the abstract attribute being optional on the class declaration itself, one has to look through all the methods to determine whether the class is abstract or not. With it being mandatory, the information is right in front of you when you look at the class.
It would also be useful for documentation generators. Whether a class has any abstract methods depends not only on methods declared directly within the body of the class, but also on those in superclasses of the class, interfaces the class implements, and method declarations generated by mixins. The last of these effectively means that no standalone documentation generator (unless it contains a full D conditional compilation and CTFE engine) can correctly indicate which classes are abstract and which aren't.
Comment #10 by doob — 2013-01-21T11:12:53Z
(In reply to comment #0)
> > On Mon, 28 Apr 2008 10:11:23 -0300, Ary Borenszweig
> A class can either be abstract or not abstract. Currently in D, if you
> don't mark a class as abstract, it can still be it if it contains an
> abstract method:
>
> class Foo {
>
> abstract void someAbstract();
>
> void nonAbstract() {
> }
>
> }
>
> When designing a class, you have in mind whether the class is going to
> be abstract or not. If it's not going to be abstract, you want the
> compiler to help you by telling you "You made a mistake. This class is
> still abstract because you didn't implement method foo".
>
> So I want to extend Foo with a class Bar, but I want Bar to be not abstract.
>
> class Bar : Foo {
> }
>
> I compile, and it gives no error, of course. But I want there to be an
> error there.
There might be a problem with this since D supports separate compilation. There can be another object file that contains the implementation of Bar.someAbstract. I'm not sure if this applies here.
Comment #11 by smjg — 2013-01-21T12:40:39Z
(In reply to comment #10)
> There might be a problem with this since D supports separate
> compilation. There can be another object file that contains the
> implementation of Bar.someAbstract. I'm not sure if this applies
> here.
No, for this to apply, Bar would need to contain its own declaration of someAbstract.
Comment #12 by andrej.mitrovich — 2013-01-21T12:55:16Z
(In reply to comment #0)
> (also, it would be nice if the compiler could tell you all the methods
> that still need an implementation, rather than just one)
This seems to be fixed now.
Comment #13 by verylonglogin.reg — 2014-07-18T11:08:07Z
1. Currently `abstract` in required if not all `interface` methods are implemented or overridden with an `abstract` method:
---
interface I { void f(); }
abstract class CI1: I { } // `abstract` required
abstract class CI2: CI1 { } // `abstract` required too
class B { abstract void f(); }
class CB: B { } // derived as `abstract`
class CI3: I { abstract void f(); }
class CI4: CI3 { } // derived as `abstract` too
---
This difference looks inconsistent.
2. When one sees a class `C` and want to understand whether or not it is abstract he has to look through every ancestor of `C` to determine whether it contains an `abstract` method unimplemented further. And it's really hard. IMO it would be much better if class declaration clearly shows whether it's abstract or not.
3. Currently a compiler prints what methods are unimplemented if one tries to create an abstract class instance so this isn't a critical issue, just a bad looking thing in a language (IMHO).
Comment #14 by robert.schadek — 2024-12-13T17:49:59Z