www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Ambiguous class definition syntax

reply Derek Parnell <derek psych.ward> writes:
This is really a petty thing, so please excuse me, ... but I'm occasionally
troubled by not knowing how a class is being derived. There is no quick way
to tell if the first identifier after the ':' is another class or an
interface. It probably doesn't matter, of course.

Anyhow, although the documentation states that the syntax is ...

 ClassDeclaration:
 'class' Identifier [SuperClass {, InterfaceClass }] ClassBody

 SuperClass:
 ':' Identifier

 InterfaceClass:
 Identifier


it should really be more like ...

 ClassDeclaration:
 'class' Identifier [ClassDerivation] ClassBody

 ClassDerivation:
 ':' (SuperClass | InterfaceClass) {',' InterfaceClass}

 SuperClass:
 Identifier

 InterfaceClass:
 Identifier

Because the first item can be either a class or an interface. The current
syntax documentation implies that it can only be a class, but that is not
correct.

I was thinking that the production ClassDerivation could be changed
slightly to ...

 ClassDerivation:
 ':' [(SuperClass | InterfaceClass)] {',' InterfaceClass}

such that we could write ...

  class Foo : ,iOne, iTwo {}

and thus explicitly show that the first identifier in the derivation list
is an interface. Of course, this wouldn't help with code written under the
current syntax rule.

Or if one really wants to get wordy...

  class Foo: parent BaseFoo, interface iOne, iTwo {}

This might also help get over the apparent confusion that a D interface is
like a lightweight abstract class, which it is not.

-- 
Derek
Melbourne, Australia
30/Jul/04 2:07:43 PM
Jul 29 2004
next sibling parent Berin Loritsch <bloritsch d-haven.org> writes:
Derek Parnell wrote:
 Or if one really wants to get wordy...
 
   class Foo: parent BaseFoo, interface iOne, iTwo {}
 
 This might also help get over the apparent confusion that a D interface is
 like a lightweight abstract class, which it is not.
Hmm, does this look familiar to anybody: class Foo extends BaseFoo implements iOne, iTwo {} ??? There are a hundred ways of doing this. I know C++ doesn't care one way or the other what the parent classes are (assuming you equate a pure virtual base class to an interface). Honestly, I think it is clumsy to force a comma for a placeholder of whether the first element of the dirivation list is not an interface. What is so bad about the way it currently _works_? If nothing, then just change the text that describes it.
Jul 30 2004
prev sibling parent reply "me" <memsom interalpha.co.uk> writes:
 Or if one really wants to get wordy...

   class Foo: parent BaseFoo, interface iOne, iTwo {}
Nah, but almost right ;-) All interfaces names should be previxed with 'I'. interface IMessageBoxInvoker { MessageBox(char[] message); } class MessageBoxInvoker : IMessageBoxInvoker { MessageBox(char[] message) {...} } That will prevent name clashes too... Matt
Jul 30 2004
parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <ceeu8i$e0s$1 digitaldaemon.com>, me says...
Nah, but almost right ;-) All interfaces names should be previxed with 'I'.
Yuck. That is so Microsoft! Interfaces in Java don't all start with "I". The only place this I-rule apprears to be true is in the Microsoft COM world, Well, the D style guide says "just say no" to hungarian notation, and I rather agree with that. Arcane Jill
Aug 01 2004
next sibling parent Andy Friesen <andy ikagames.com> writes:
Arcane Jill wrote:

 In article <ceeu8i$e0s$1 digitaldaemon.com>, me says...
 
Nah, but almost right ;-) All interfaces names should be previxed with 'I'.
Yuck. That is so Microsoft! Interfaces in Java don't all start with "I". The only place this I-rule apprears to be true is in the Microsoft COM world, Well, the D style guide says "just say no" to hungarian notation, and I rather agree with that.
Their .NET coding standard says to prefix interfaces with I as well. (a peculiar exception to the rule, incidently: except for IInterfaces, pretty much everything is to be written using PascalCase, except for privateAttributes and functionParameters) -- andy
Aug 01 2004
prev sibling parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"Arcane Jill" <Arcane_member pathlink.com> wrote in message
news:ceiu0n$245s$1 digitaldaemon.com...
 In article <ceeu8i$e0s$1 digitaldaemon.com>, me says...
Nah, but almost right ;-) All interfaces names should be previxed with 'I'.
Yuck. That is so Microsoft! Interfaces in Java don't all start with "I". The only place this I-rule apprears to be true is in the Microsoft COM world, Well, the D style guide says "just say no" to hungarian notation, and I rather agree with that. Arcane Jill
First, AFAIK, 'I' is also used for interfaces in .NET, so your assertion is not correct. (And I've seen plenty of non-COM C++ code in various clients' code bases.) If something emanates from Microsoft (or any other specific vendor), does that make it automatically bad? That is a pathetic attitude. Finally, Hungarian is about decorating _variable_ names with type. The 'I' is part of the _type_ name.
Aug 01 2004