comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: orthogonal inheritance and extension aggregates
Date: Fri, 19 Jul 2013 21:05:54 +0200
Date: 2013-07-19T21:05:54+02:00	[thread overview]
Message-ID: <igo2b70i4ox.t5duix9weguc$.dlg@40tude.net> (raw)
In-Reply-To: ksbu1u$75k$1@online.de

On Fri, 19 Jul 2013 19:46:38 +0200, Felix Krause wrote:

> I try to implement some feature via orthogonal inhertance like this:
> 
>     generic
>        type Parent (<>) is abstract tagged private;
>     package Extension is
>        type Extended is abstract new Parent with private;
> 
>        -- operations of the extended type here
>     private
>        type Extended is abstract new Parent with record
>           -- …
>        end record;
>     end Extension;
> 
> Now for testing, I tried to apply this on a Vector:
> 
>     package Int_Vectors is new Ada.Container.Vectors (Natural, Integer);
>     package Base is new Extension (Int_Vectors.Vector);
> 
>     type Child is new Base.Extended with null record;
> 
> At the declaration of Child, GNAT tells me "type must be declared 
> abstract or "To_Vector" overridden". Can someone explain why this 
> happens? As I understand it, To_Vector should be automatically defined 
> for the derived type. Is this because the intermediate type Extended is 
> declared abstract? Or because To_Vector dispatches in the return type?
> 
> Anyway, I tried to declare the function for the type Child like this:
> 
> 
>     function To_Vector (Length : Ada.Containers.Count_Type) return Child is
>     begin
>        return Child'(Base.Extended'(Int_Vectors.To_Vector (Length) with 
> others => <>) with null record);
>     end To_Vector;
> 
> Firstly, this looks rather awkward. Secondly, it doesn't compile: 
> "expected private type Extended […] found a composite type". I guess 
> this does not work because the type Extended is hidden in the private 
> part of Extension. Is it possible to define To_Vector in this context 
> to do exactly what the base function does?

OK here are two different issues.

1. There are more than one To_Vector declared in Ada.Containers.Vectors.
And there are other primitive operations there you must override:

with Ada.Containers.Vectors;
with Extension;
package PE is
    package Int_Vectors is new Ada.Containers.Vectors (Natural, Integer);
    package Base is new Extension (Int_Vectors.Vector);

    type Child is new Base.Extended with null record;
    overriding
       function To_Vector (Length : Ada.Containers.Count_Type) return
Child;
    overriding
       function To_Vector
                (  New_Item : Integer;
                   Length   : Ada.Containers.Count_Type
		)  return Child;	   
    overriding
       function "&" (Left : Child; Right : Child) return Child;
    overriding
       function "&" (Left : Integer; Right : Child) return Child;
    overriding
       function "&" (Left : Child; Right : Integer) return Child;
    overriding
       function "&" (Left, Right : Integer) return Child;	   
    overriding
       function Copy
                (  Source : Child;
                   Capacity : Ada.Containers.Count_Type := 0
                )  return Child;
end PE;

2. This is a more serious problem. You use a private extension in
Extension, which means that clients cannot use aggregates in order to
create instances.

Extension aggregates is a horrific thing in Ada. But nothing compared to
the constructing function.

At this point you must consider what are you going to fight. If aggregates,
then make extension public. If functions then provide a constructing
function in the Extension package.

Beware that your case will be a *combination* of aggregates and
constructing functions is totally broken in Ada. It might happen to work to
you or not. Nobody, even a language lawyer could say in advance if and when
it will.

P.S. I don't know if Ada 2012 silently inherits from null extension. I
heard that there was an intention to add the kludge. Though semantically
broken it would save programs like yours.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  parent reply	other threads:[~2013-07-19 19:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-19 17:46 orthogonal inheritance and extension aggregates Felix Krause
2013-07-19 18:00 ` Adam Beneschan
2013-07-22 16:02   ` Adam Beneschan
2013-07-22 18:44     ` Dmitry A. Kazakov
2013-07-22 18:59     ` Adam Beneschan
2013-07-22 19:19       ` Adam Beneschan
2013-07-22 22:03       ` Felix Krause
2013-07-19 19:05 ` Dmitry A. Kazakov [this message]
2013-07-20  5:49   ` Randy Brukardt
2013-07-20  6:36     ` Dmitry A. Kazakov
2013-07-20  7:54       ` Niklas Holsti
replies disabled

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