Jump to page: 1 26  
Page
Thread overview
D Project Status
Aug 30, 2001
Michael Lee Yohe
Aug 30, 2001
Walter
How to avoid code duplication in D? MI/CI/delegation/C&P?
Aug 30, 2001
Yu Qian Zhou
Aug 30, 2001
Walter
Aug 30, 2001
Dan Hursh
Sep 01, 2001
Walter
Sep 02, 2001
Yu Qian Zhou
Sep 02, 2001
Walter
Sep 02, 2001
Axel Kittenberger
Sep 02, 2001
kaffiene
Sep 02, 2001
Axel Kittenberger
Sep 02, 2001
Dan Hursh
Sep 02, 2001
Axel Kittenberger
Sep 03, 2001
Dan Hursh
Sep 03, 2001
Axel Kittenberger
Sep 03, 2001
Walter
Sep 03, 2001
Dan Hursh
Division by zero
Oct 24, 2001
Sean L. Palmer
Oct 24, 2001
Axel Kittenberger
Oct 24, 2001
Russ Lewis
Sep 07, 2001
Iliya Peregoudov
Sep 08, 2001
Walter
Sep 09, 2001
Iliya Peregoudov
Sep 09, 2001
Axel Kittenberger
Sep 09, 2001
Iliya Peregoudov
Sep 09, 2001
Axel Kittenberger
Sep 09, 2001
Walter
Sep 09, 2001
Axel Kittenberger
Sep 09, 2001
Walter
Sep 10, 2001
Axel Kittenberger
Sep 11, 2001
a
Sep 10, 2001
Iliya Peregoudov
Sep 08, 2001
Axel Kittenberger
Sep 09, 2001
Iliya Peregoudov
Sep 02, 2001
Bradeeoh
Sep 02, 2001
Charles D Hixson
Sep 03, 2001
Angus Graham
Aug 30, 2001
Yu Qian Zhou
Aug 30, 2001
Richard Krehbiel
Aug 30, 2001
Walter
Aug 31, 2001
Dan Hursh
Aug 30, 2001
Dan Hursh
Aug 30, 2001
Eric Gerlach
Aug 30, 2001
Axel Kittenberger
Aug 31, 2001
Dan Hursh
Oct 24, 2001
Sean L. Palmer
Oct 24, 2001
Jan Knepper
Please do check this URL about MI! [was] Re: How to avoid code duplication in D? MI/CI/delegation/C&P?
Oct 27, 2001
xiaochuang ling
Oct 27, 2001
xiaochuang ling
Please do check this URL about MI! [was] Re: How to avoid code duplication in D? MI/CI/delegation/C&P?
Oct 27, 2001
xiaochuang ling
Oct 27, 2001
a
http://www.eiffel.com/doc/online/eiffel50/intro/language/tutorial-00.html (then click on Section 9) [was] Eiffel MI
Oct 27, 2001
xiaochuang ling
Oct 27, 2001
a
Oct 28, 2001
xiaochuang ling
Aug 30, 2001
Michael Lee Yohe
Aug 31, 2001
Chris Friesen
Aug 31, 2001
Walter
August 30, 2001
I would like to hear from the persons responsible for bringing the world "D" the status of the project, compiler, and the amount of effort being put into play to expand and define the language.  While it looks to be quite promising, there is something sweet about a language that has existed for 20+ years and is still being actively used.  This is largely due to the amount of effort (from many, many people) into C.  I'm anxious to see if this is vaporware, or if we're dealing something that is serious enough to contribute time to.

-- 

Michael Lee Yohe (myohe+USENET@redhat.com)
Software Developer, Engineering Services
Red Hat, Inc.

QUIPd 0.12:
-> One man with courage is a majority.
-> - Thomas Jefferson (1743-1826)

August 30, 2001
The compiler exists, but is a bit buggy. The runtime library is a bit embryonic. The other tools - linker, make, etc., are all done & mature since they come from the C/C++ compiler.

I'm currently working on building a better garbage collector.

Michael Lee Yohe wrote in message <3B8D9A7A.90404@redhat.com>...
>
>I would like to hear from the persons responsible for bringing the world "D" the status of the project, compiler, and the amount of effort being put into play to expand and define the language.  While it looks to be quite promising, there is something sweet about a language that has existed for 20+ years and is still being actively used.  This is largely due to the amount of effort (from many, many people) into C.  I'm anxious to see if this is vaporware, or if we're dealing something that is serious enough to contribute time to.



August 30, 2001
Admittedly Java's single inheritance is easy to implement, and simple to use.  But it does have some defect which sometimes force a programmer to duplicate code by 'copy & paste':

Suppose I have two classes 'Car' and 'Dog', which *have already inherited* from some more important classes 'Vehicle' and 'Animal' respectively (let's assume that 'Vehicle' and 'Dog' do not have a common modifiable ancestor); now I want both these two classes to implement another interface 'Idable', how can I do it?

Answer:
	  Java: Copy & Paste,               code duplication !!!
	   C++: Multiple inheritance,    no code duplication
	   C++: Textual code inclusion,  no code duplication
	Eiffel: Multiple inheritance,    no code duplication
	Sather: Semantic code inclusion, no code duplication
          Kiev: MI by delegation,        no code duplication

so what's the solution D can provide?

	     D: ???                         code duplication ???

D borrowed DbC from Eiffel, perhaps it also worth to check the multiple inheritance mechanism of Eiffel, which is much cleaner than C++ (by renaming and redefinition).

Or if you still feel strongly against MI, maybe you can also consider Sather's semantic code inclusion, or Kiev's forward-able delegation?

Let me know your thoughts on this.

YuQian


Sample code:
/***********************************************************************
  Java: Copy & Paste, code duplication
***********************************************************************/
interface Idable
{
  public   void setId(int i);
  public   void getId();
}

class      Car
extends    Vehicle
implements Idable
{
  private  int id;
  public   void setId(int i) { id = i;   }
  public   void getId()      { return i; }
}


class      Dog
extends    Animal
implements Idable
{
  private  int id;				// copy
  public   void setId(int i) { id = i;   }    	// &
  public   void getId()      { return i; }    	// paste!
}

/***********************************************************************
  Kiev:  Multiple inheritance by delegation, no code duplication
***********************************************************************/
class Id
{
private int id;
public  void setId(int i) { id = i;   }
public  void getId()      { return i; }
}

class   Car
extends Vehicle
{
    forward public Id myId;   // forward car.getId() to car.myId.getId()
}

class   Dog
extends Animal
{
    forward public Id myId;   // forward dog.getId() to dog.myId.getId()
}

/***********************************************************************
  C++:  Multiple inheritance, no code duplication
***********************************************************************/
class Idable
{
private: int id;
public:
   void setId(int i) { id = i;   }
   void getId()      { return i; }
};

class Car: public Vehicle, Idable {};
class Dog: public Animal,  Idable {};

/***********************************************************************
  C++:  Textual code inclusion, no code duplication
***********************************************************************/

//--- begin file: id.ci ---
private: int id;
public:
   void setId(int i) { id = i;   }
   void getId()      { return i; }
//--- end   file: id.ci ---


class Car: public Vehicle
{
#include "id.ci"
};

class Dog: public Animal
{
#include "id.ci"
};


August 30, 2001
The way to do it is to use interfaces:

    class Car : Vehicle, Idable
    {
    }

Base classes after the first base class are interface classes.

-Walter

-----------------------------------------------------------------

Yu Qian Zhou wrote in message ...

Admittedly Java's single inheritance is easy to implement, and simple to use.  But it does have some defect which sometimes force a programmer to duplicate code by 'copy & paste':

Suppose I have two classes 'Car' and 'Dog', which *have already inherited* from some more important classes 'Vehicle' and 'Animal' respectively (let's assume that 'Vehicle' and 'Dog' do not have a common modifiable ancestor); now I want both these two classes to implement another interface 'Idable', how can I do it?

Answer:
  Java: Copy & Paste,               code duplication !!!
   C++: Multiple inheritance,    no code duplication
   C++: Textual code inclusion,  no code duplication
Eiffel: Multiple inheritance,    no code duplication
Sather: Semantic code inclusion, no code duplication
          Kiev: MI by delegation,        no code duplication

so what's the solution D can provide?

     D: ???                         code duplication ???

D borrowed DbC from Eiffel, perhaps it also worth to check the multiple inheritance mechanism of Eiffel, which is much cleaner than C++ (by renaming and redefinition).

Or if you still feel strongly against MI, maybe you can also consider Sather's semantic code inclusion, or Kiev's forward-able delegation?

Let me know your thoughts on this.

YuQian


Sample code:
/***********************************************************************
  Java: Copy & Paste, code duplication
***********************************************************************/
interface Idable
{
  public   void setId(int i);
  public   void getId();
}

class      Car
extends    Vehicle
implements Idable
{
  private  int id;
  public   void setId(int i) { id = i;   }
  public   void getId()      { return i; }
}


class      Dog
extends    Animal
implements Idable
{
  private  int id; // copy
  public   void setId(int i) { id = i;   }    // &
  public   void getId()      { return i; }    // paste!
}

/***********************************************************************
  Kiev:  Multiple inheritance by delegation, no code duplication
***********************************************************************/
class Id
{
private int id;
public  void setId(int i) { id = i;   }
public  void getId()      { return i; }
}

class  Car
extends Vehicle
{
forward public Id myId;   // forward car.getId() to car.myId.getId()
}

class  Dog
extends Animal
{
forward public Id myId;   // forward dog.getId() to dog.myId.getId()
}

/***********************************************************************
  C++:  Multiple inheritance, no code duplication
***********************************************************************/
class Idable
{
private: int id;
public:
   void setId(int i) { id = i;   }
   void getId()      { return i; }
};

class Car: public Vehicle, Idable {};
class Dog: public Animal,  Idable {};

/***********************************************************************
  C++:  Textual code inclusion, no code duplication
***********************************************************************/

//--- begin file: id.ci ---
private: int id;
public:
   void setId(int i) { id = i;   }
   void getId()      { return i; }
//--- end   file: id.ci ---


class Car: public Vehicle
{
#include "id.ci"
};

class Dog: public Animal
{
#include "id.ci"
};




August 30, 2001
	I believe the D way is code duplication.  I personally don't like this,
but it was a design decision in order to prevent the inheritance mess
that C++ owes us an apology for.  I know C++'s syntax and semantics are
rather nasty and I hear it makes the compiler internals a mess so I
can't complain if MI is left out.  I just dislike how some folk pretend
that there isn't a loss of functionality without MI.
	I don't know if D will support MI since there has been a strong
reaction against, but if you know of a better way that it could be
implemented without sacrificing the other design goals of D (including
an easy to implement compiler), I love to hear it.  Since the time that
I only knew fortran 77 and Commodore basic, I've want MI functionality.
I just didn't know what it was called until I learn C++.

Dan


Yu Qian Zhou wrote:
> 
> Admittedly Java's single inheritance is easy to implement, and simple to use.  But it does have some defect which sometimes force a programmer to duplicate code by 'copy & paste':
> 
> Suppose I have two classes 'Car' and 'Dog', which *have already inherited* from some more important classes 'Vehicle' and 'Animal' respectively (let's assume that 'Vehicle' and 'Dog' do not have a common modifiable ancestor); now I want both these two classes to implement another interface 'Idable', how can I do it?
> 
> Answer:
>           Java: Copy & Paste,               code duplication !!!
>            C++: Multiple inheritance,    no code duplication
>            C++: Textual code inclusion,  no code duplication
>         Eiffel: Multiple inheritance,    no code duplication
>         Sather: Semantic code inclusion, no code duplication
>           Kiev: MI by delegation,        no code duplication
> 
> so what's the solution D can provide?
> 
>              D: ???                         code duplication ???
> 
> D borrowed DbC from Eiffel, perhaps it also worth to check the multiple inheritance mechanism of Eiffel, which is much cleaner than C++ (by renaming and redefinition).
> 
> Or if you still feel strongly against MI, maybe you can also consider Sather's semantic code inclusion, or Kiev's forward-able delegation?
> 
> Let me know your thoughts on this.
> 
> YuQian
> 
> Sample code:
> /***********************************************************************
>   Java: Copy & Paste, code duplication
> ***********************************************************************/
> interface Idable
> {
>   public   void setId(int i);
>   public   void getId();
> }
> 
> class      Car
> extends    Vehicle
> implements Idable
> {
>   private  int id;
>   public   void setId(int i) { id = i;   }
>   public   void getId()      { return i; }
> }
> 
> class      Dog
> extends    Animal
> implements Idable
> {
>   private  int id;                              // copy
>   public   void setId(int i) { id = i;   }      // &
>   public   void getId()      { return i; }      // paste!
> }
> 
> /***********************************************************************
>   Kiev:  Multiple inheritance by delegation, no code duplication
> ***********************************************************************/
> class Id
> {
> private int id;
> public  void setId(int i) { id = i;   }
> public  void getId()      { return i; }
> }
> 
> class   Car
> extends Vehicle
> {
>     forward public Id myId;   // forward car.getId() to car.myId.getId()
> }
> 
> class   Dog
> extends Animal
> {
>     forward public Id myId;   // forward dog.getId() to dog.myId.getId()
> }
> 
> /***********************************************************************
>   C++:  Multiple inheritance, no code duplication
> ***********************************************************************/
> class Idable
> {
> private: int id;
> public:
>    void setId(int i) { id = i;   }
>    void getId()      { return i; }
> };
> 
> class Car: public Vehicle, Idable {};
> class Dog: public Animal,  Idable {};
> 
> /***********************************************************************
>   C++:  Textual code inclusion, no code duplication
> ***********************************************************************/
> 
> //--- begin file: id.ci ---
> private: int id;
> public:
>    void setId(int i) { id = i;   }
>    void getId()      { return i; }
> //--- end   file: id.ci ---
> 
> class Car: public Vehicle
> {
> #include "id.ci"
> };
> 
> class Dog: public Animal
> {
> #include "id.ci"
> };
August 30, 2001
Walter wrote:
> 
> The way to do it is to use interfaces:
> 
>     class Car : Vehicle, Idable
>     {
>     }
> 
> Base classes after the first base class are interface classes.
> 
> -Walter

Now I'm confused.  Can a class inherit implementation code from an interface?  If so then I've lost the difference between an interface and multiple inheritance.  This also doesn't handle the case where Vehicle and Idable are existing classes (from a library?) that you would rather reuse instead of re-writing.

Dan
August 30, 2001
> The way to do it is to use interfaces:
> 
>     class Car : Vehicle, Idable
>     {
>     }
> 
> Base classes after the first base class are interface classes.
>
>-Walter

What exactly do you mean?

	"The way to do it is to use interfaces ... "

	... to *introduce* code duplication by Copy&Paste like Java ?

But my question is

	"How to *avoid* ..."

	... code duplication in D ?

:-)

YuQian


August 30, 2001
Interfaces are fine and dandy, but the problem is, an interface doesn't carry any *code* with it.  Just an interface spec.

A Java example:

interface A
{
    void DoRight(int a);    // Declares that any class that implements A
                            // will provide DoRight.  No implementation.
}

class Z implements A
{
    void DoRight(int a)
    {
        // Imagine here 160 lines of complex implementation code.
    }
}

class Y implements A
{
    void DoRight(int a)
    {
        // 160 lines of complex implementation code.
        // No matter that it may be *identical* to Z.DoRight...
    }
}

Duplication of source code.  The worst possible thing for maintenance.

A way to solve this in Java is to declare *another* class...

class H
{
    public void DoRight(int a)
    {
        // 160 lines of, well, you know
    }
}

class Z implements A
{
    H helper = new H;
    public void DoRight(int a)
    {
        helper.DoRight(a);
    }
}

class Y implements A
{
    H helper = new H;
    void DoRight(int a)
    {
        helper.DoRight(a);
    }
}

This is less convenient than multiple inheritance.

This could be alleviated if A.DoRight could have a default implementation. But since an interface cannot declare any instance variables, this may be somewhat troublesome.

Why not allow interfaces in D to have instance data, and interface methods to have implementations?  Then it would be as convenient as multiple inheritance.  Interfaces would still be distinct from classes, so it's not *quite* M-I.

"Walter" <walter@digitalmars.com> wrote in message news:9mkido$q2i$1@digitaldaemon.com...
> The way to do it is to use interfaces:
>
>     class Car : Vehicle, Idable
>     {
>     }
>
> Base classes after the first base class are interface classes.
>
> -Walter
>
> -----------------------------------------------------------------
>
> Yu Qian Zhou wrote in message ...
>
> Admittedly Java's single inheritance is easy to implement, and simple to use.  But it does have some defect which sometimes force a programmer to duplicate code by 'copy & paste':
>
> Suppose I have two classes 'Car' and 'Dog', which *have already inherited* from some more important classes 'Vehicle' and 'Animal' respectively (let's assume that 'Vehicle' and 'Dog' do not have a common modifiable ancestor); now I want both these two classes to implement another interface 'Idable', how can I do it?
>
> Answer:
>   Java: Copy & Paste,               code duplication !!!
>    C++: Multiple inheritance,    no code duplication
>    C++: Textual code inclusion,  no code duplication
> Eiffel: Multiple inheritance,    no code duplication
> Sather: Semantic code inclusion, no code duplication
>           Kiev: MI by delegation,        no code duplication
>
> so what's the solution D can provide?
>
>      D: ???                         code duplication ???
>

--
Richard Krehbiel, Arlington, VA, USA
rich@kastle.com (work) or krehbiel3@home.com (personal)


August 30, 2001
>  I believe the D way is code duplication.  I personally don't like this,
> but it was a design decision in order to prevent the inheritance mess
> that C++ owes us an apology for.  I know C++'s syntax and semantics are
> rather nasty and I hear it makes the compiler internals a mess so I
> can't complain if MI is left out.  I just dislike how some folk pretend
> that there isn't a loss of functionality without MI.
> 	I don't know if D will support MI since there has been a strong
> reaction against, but if you know of a better way that it could be
> implemented without sacrificing the other design goals of D (including
> an easy to implement compiler), I love to hear it.  Since the time that
> I only knew fortran 77 and Commodore basic, I've want MI functionality. I just didn't know what it was called until I learn C++.

I was thinking about this this morning... you *do* lose a nice feature without MI.  The real reason that I think that's the case is because it's a bitch to get it right.  There are so may cases of conflicting functions... how do you resolve them and know which function to call?

Suppose classes A and B both define foo().  C inherits from both A and B, which one do you inherit?  What if foo() in A and B were redefined from a common ancestor, Z?  What if one of them is final?  There's just too much to think about.

Here's my thought:  What about allowing MI, as long as there are *no* conflicts?  None.  Whatsoever.  If there are, it's an error.  That allows the useful part of MI without getting into the mess.

Now, if anyone has a good way of resolving conflicts (I hear Eiffel is good at that) I think we'd all be willing to hear it.  But disallowing conflicts shouldn't affect the compiler too much (it just fills in the vtable it was going to leave blank), and it gives ua a nice feature to play with.

Comments on that?

Eric

August 30, 2001
Unfortunately, the helper class is the way to factor out the common code. Another way is to embed an instance of A as a member of Y and Z. -Walter

"Richard Krehbiel" <rich@kastle.com> wrote in message news:9ml9gr$1844$1@digitaldaemon.com...
> Interfaces are fine and dandy, but the problem is, an interface doesn't carry any *code* with it.  Just an interface spec.
>
> A Java example:
>
> interface A
> {
>     void DoRight(int a);    // Declares that any class that implements A
>                             // will provide DoRight.  No implementation.
> }
>
> class Z implements A
> {
>     void DoRight(int a)
>     {
>         // Imagine here 160 lines of complex implementation code.
>     }
> }
>
> class Y implements A
> {
>     void DoRight(int a)
>     {
>         // 160 lines of complex implementation code.
>         // No matter that it may be *identical* to Z.DoRight...
>     }
> }
>
> Duplication of source code.  The worst possible thing for maintenance.
>
> A way to solve this in Java is to declare *another* class...
>
> class H
> {
>     public void DoRight(int a)
>     {
>         // 160 lines of, well, you know
>     }
> }
>
> class Z implements A
> {
>     H helper = new H;
>     public void DoRight(int a)
>     {
>         helper.DoRight(a);
>     }
> }
>
> class Y implements A
> {
>     H helper = new H;
>     void DoRight(int a)
>     {
>         helper.DoRight(a);
>     }
> }
>
> This is less convenient than multiple inheritance.
>
> This could be alleviated if A.DoRight could have a default implementation. But since an interface cannot declare any instance variables, this may be somewhat troublesome.
>
> Why not allow interfaces in D to have instance data, and interface methods to have implementations?  Then it would be as convenient as multiple inheritance.  Interfaces would still be distinct from classes, so it's not *quite* M-I.
>
> "Walter" <walter@digitalmars.com> wrote in message news:9mkido$q2i$1@digitaldaemon.com...
> > The way to do it is to use interfaces:
> >
> >     class Car : Vehicle, Idable
> >     {
> >     }
> >
> > Base classes after the first base class are interface classes.
> >
> > -Walter
> >
> > -----------------------------------------------------------------
> >
> > Yu Qian Zhou wrote in message ...
> >
> > Admittedly Java's single inheritance is easy to implement, and simple to use.  But it does have some defect which sometimes force a programmer to duplicate code by 'copy & paste':
> >
> > Suppose I have two classes 'Car' and 'Dog', which *have already
inherited*
> > from some more important classes 'Vehicle' and 'Animal' respectively (let's assume that 'Vehicle' and 'Dog' do not have a common modifiable ancestor); now I want both these two classes to implement another interface 'Idable', how can I do it?
> >
> > Answer:
> >   Java: Copy & Paste,               code duplication !!!
> >    C++: Multiple inheritance,    no code duplication
> >    C++: Textual code inclusion,  no code duplication
> > Eiffel: Multiple inheritance,    no code duplication
> > Sather: Semantic code inclusion, no code duplication
> >           Kiev: MI by delegation,        no code duplication
> >
> > so what's the solution D can provide?
> >
> >      D: ???                         code duplication ???
> >
>
> --
> Richard Krehbiel, Arlington, VA, USA
> rich@kastle.com (work) or krehbiel3@home.com (personal)
>
>


« First   ‹ Prev
1 2 3 4 5 6