www.digitalmars.com         C & C++   DMDScript  

D - explicit abstract-class syntax

reply "Kris" <someidiot earthlink.net> writes:
abstract class Foo
{
}

main()
{
    Foo foo = new Foo();
}

Shouldn't this example generate a compile error? I thought I'd try to get
around the "Partial-Interface Implementation" issue by attempting the
following wacky hack <g>:

interface Bar
{
    void bar();
}

abstract class Foo : Bar   // force class to be abstract?
{
    abstract void bar(){}; // hackety hack
}

main()
{
    Foo foo = new Foo();
    foo.bar();
}

This compiles and executes fine. Apparently it's perfectly legal to
instantiate a class explicitly declared as abstract. Only an abstract
method, sans body, causes the compiler to emit the expected error. I guess
the "abstract class" itself is just syntactically ignored?
Feb 25 2004
next sibling parent reply "Walter" <walter digitalmars.com> writes:
"Kris" <someidiot earthlink.net> wrote in message
news:c1iu6b$2v8f$1 digitaldaemon.com...
 This compiles and executes fine. Apparently it's perfectly legal to
 instantiate a class explicitly declared as abstract. Only an abstract
 method, sans body, causes the compiler to emit the expected error. I guess
 the "abstract class" itself is just syntactically ignored?
That's right. A class is abstract if and only if it has any abstract member functions. I should fix the documentation...
Feb 25 2004
next sibling parent reply "Kris" <someidiot earthlink.net> writes:
Note also that an abstract attribute on a method is ignored if said method
has a body ...

- Kris

"Walter" <walter digitalmars.com> wrote in message
news:c1j4d8$8nd$1 digitaldaemon.com...
 "Kris" <someidiot earthlink.net> wrote in message
 news:c1iu6b$2v8f$1 digitaldaemon.com...
 This compiles and executes fine. Apparently it's perfectly legal to
 instantiate a class explicitly declared as abstract. Only an abstract
 method, sans body, causes the compiler to emit the expected error. I
guess
 the "abstract class" itself is just syntactically ignored?
That's right. A class is abstract if and only if it has any abstract
member
 functions. I should fix the documentation...
Feb 25 2004
parent reply Sean Kelly <sean ffwd.cx> writes:
Kris wrote:
 Note also that an abstract attribute on a method is ignored if said method
 has a body ...
So this is an instance where "abstract" differs from the "=0" semantics in C++. If we already have abstract classes, why not allow for this: interface IFace { void a(); void b(); } abstract class AClass : IFace { void a() {} abstract b(); } class BClass : AClass { void b() {} }
Feb 25 2004
parent "Kris" <someidiot earthlink.net> writes:
Exactly -- clean and elegant (although the 'abstract' attribute on AClass
itself would be superfluous in D, and is indeed silently ignored).

Check out the nasty hack to achieve the same thing, in "Partial
implementation of Interfaces (Walter?)"

- Kris


"Sean Kelly" <sean ffwd.cx> wrote in message
news:c1j8i9$flq$1 digitaldaemon.com...
 Kris wrote:
  >
 Note also that an abstract attribute on a method is ignored if said
method
 has a body ...
So this is an instance where "abstract" differs from the "=0" semantics in C++. If we already have abstract classes, why not allow for this: interface IFace { void a(); void b(); } abstract class AClass : IFace { void a() {} abstract b(); } class BClass : AClass { void b() {} }
Feb 25 2004
prev sibling next sibling parent "Andres Rodriguez" <rodriguez ai.sri.com> writes:
What is the point of declaring classes with the "abstract" keyword
then?  And it is not a rethorical question, I really don't know the
answer.


"Walter" <walter digitalmars.com> wrote in message
news:c1j4d8$8nd$1 digitaldaemon.com...
 "Kris" <someidiot earthlink.net> wrote in message
 news:c1iu6b$2v8f$1 digitaldaemon.com...
 This compiles and executes fine. Apparently it's perfectly legal to
 instantiate a class explicitly declared as abstract. Only an abstract
 method, sans body, causes the compiler to emit the expected error. I
guess
 the "abstract class" itself is just syntactically ignored?
That's right. A class is abstract if and only if it has any abstract
member
 functions. I should fix the documentation...
Feb 25 2004
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Walter wrote:

 "Kris" <someidiot earthlink.net> wrote in message
 news:c1iu6b$2v8f$1 digitaldaemon.com...
 
This compiles and executes fine. Apparently it's perfectly legal to
instantiate a class explicitly declared as abstract. Only an abstract
method, sans body, causes the compiler to emit the expected error. I guess
the "abstract class" itself is just syntactically ignored?
That's right. A class is abstract if and only if it has any abstract member functions. I should fix the documentation...
So, why have the abstract keyword if it means nothing? Java's implementation is robust in my mind: - declaring a method as 'abstract' and giving it a body at the same time is an error - the 'abstract class' syntactic salt is required - a class can be declared abstract, and consequently actually be abstract, even if it has no abstract methods I suppose you *could* deprecate this lack of salt in D. But even if not, the compiler ought to pinpoint the cause of a 'cannot instantiate abstract class' error. I once drove myself mad trying to find one of these, when I had simply forgotten to implement an abstract method from the base class. Borland C++ does this, with a double error like Error: Cannot create instance of abstract class 'Qwert'. Error: Class 'Qwert' is abstract because of pure virtual function 'yuiop'. I forget if the second line referenced the declaration of the pure virtual function (abstract method).... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Feb 26 2004
prev sibling parent "Ben Hinkle" <bhinkle4 juno.com> writes:
"Kris" <someidiot earthlink.net> wrote in message
news:c1iu6b$2v8f$1 digitaldaemon.com...
| abstract class Foo
| {
| }
|
| main()
| {
|     Foo foo = new Foo();
| }
|
| Shouldn't this example generate a compile error? I thought I'd try to get
| around the "Partial-Interface Implementation" issue by attempting the
| following wacky hack <g>:
|
| interface Bar
| {
|     void bar();
| }
|
| abstract class Foo : Bar   // force class to be abstract?
| {
|     abstract void bar(){}; // hackety hack
| }
|
| main()
| {
|     Foo foo = new Foo();
|     foo.bar();
| }
|
| This compiles and executes fine. Apparently it's perfectly legal to
| instantiate a class explicitly declared as abstract. Only an abstract
| method, sans body, causes the compiler to emit the expected error. I guess
| the "abstract class" itself is just syntactically ignored?

I agree with you that if it is syntactically valid to mark a
class as abstract it should be abstract, and I assume Walter will
fix that one day, but in practice it's common to name abstract
classes with the "Abstract" prefix and so I actually hardly ever
accidentally try to instatiate an abstract class. In fact I
can't remember ever trying to instantiate something named
"AbstractFoo". I find abstract classes are rare enough that
people don't look for the abstract attribute and so the "Abstract"
prefix makes the class much more usable since people read the
class name every time. Perhaps that could be put in the D
style guide.

-Ben
Feb 26 2004