comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: class-wide objects
Date: 1998/12/10
Date: 1998-12-10T00:00:00+00:00	[thread overview]
Message-ID: <m31zm84g2m.fsf@mheaney.ni.net> (raw)
In-Reply-To: 366EE6BA.7D586BDD@magic.fr

Francois Godme <fgodme@magic.fr> writes:

> Matthew Heaney <matthew_heaney@acm.org> writes:
> 
> > Your type will probably have to have some sort of dispatching clone method:
> >
> > generic
> >   ...
> > package Stacks is
> >
> >    type Stack_Type is tagged limited private;
> >
> >    type Stack_Access is access all Stack_Type'Class;
> >
> >    function Copy (Stack : Stack_Type) return Stack_Access;
> > ...
> > end Stacks;
> >
> > which you'd use something like:
> >
> > procedure Op (Stack : in Stack_Type'Class) is
> >
> >   Copy_Of_Stack : Stack_Access := Copy (Stack);
> >
> > begin
> 
> The clone method is inventive but it is a very odd method in the sense
> that when you inherit it, it is always wrong for you. So, you have to
> redefine it.

Then just make it non-primitive:

generic
   ...
package Stacks is

   type Stack_Type is ...
...
   package Constructors is

      function  Copy (Stack : Stack_Type) return Stack_Access;

   end;
...
end Stacks;


Because Copy is non-primitive, it won't get inherited during derivation.

 
> Usually, when you redefine a method, you call the inherited method on
> the superclass part of you and finish the job yourself for the added
> part. Here, the inherited method is not reusable because it clones
> objects of your superclass not of yourself. You are the only one able
> to clone yourself as your father knows nothing of you.

In general, constructors shouldn't be primitive.

> You are not forced to override this method by the language like the
> language would do for an abstract method, so you can forget to do
> it. You are on your own.

Unless you make the operation non-primitive.  
 
> It's like a genetic disease that affects all generation. It can be
> cured easily in one generation but the next generation will be
> affected the same way. The disease cannot be stopped. It reappears
> from generation to generation. Some generation are sane, some are
> are sterile (they cannot reproduce themselves, they reproduce as their
> parents.)

There is no problem, if the operation isn't primitive.


> The cure is always the same. It can be as simple as an Emacs Macro.
> 
>    function Copy (Stack : Stack_Type) return Stack_Access is
>       use type Ada.Tags.Tag;
>    begin
>       -- tries to be kind with sick children
>       if Stack_Type'Class (Stack)'Tag /= Stack_Type'Tag then
>          raise ...; -- Is that kindness ? I think it's better than return
> null.
>                     -- The family takes on responsabilities for its sick
>                     -- children.
> 
>       else
>   return new Stack_Type;
>       end if;
>    end Copy;
> 
> The attribute I was asking for does almost the same thing as the clone method
> but without the associated problems.


There are no problems with the approach I suggest above.




  reply	other threads:[~1998-12-10  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-07  0:00 class-wide objects Francois Godme
1998-12-07  0:00 ` Tom Moran
1998-12-07  0:00 ` Matthew Heaney
1998-12-06  0:00   ` David Botton
1998-12-09  0:00   ` Francois Godme
1998-12-10  0:00     ` Matthew Heaney [this message]
1998-12-10  0:00       ` Francois Godme
1998-12-11  0:00       ` Stephen Leake
1998-12-12  0:00         ` Francois Godme
1998-12-07  0:00 ` Tucker Taft
1998-12-07  0:00   ` Francois Godme
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox