comp.lang.ada
 help / color / mirror / Atom feed
From: Jere <jhb.chat@gmail.com>
Subject: Re: Having problems instantiating a child class with an extension aggregate
Date: Sun, 26 Jan 2020 17:04:39 -0800 (PST)
Date: 2020-01-26T17:04:39-08:00	[thread overview]
Message-ID: <46cfccd5-a4be-46a0-8fa0-c3b6842842ed@googlegroups.com> (raw)
In-Reply-To: <79386e8a-20cb-4bcb-a60a-531919ee6976@googlegroups.com>

On Sunday, January 26, 2020 at 7:22:13 PM UTC-5, b.mcgui...@gmail.com wrote:
> I have three packages:
> 
> <SNIPPED>
> 
> I am trying to create instances of APL_Data_Array in apl_data_arrays.adb.  I have procedures that perform operations on one or two arrays and generate a third array with the same dimensions as one of the original arrays, so I want to create a result array, copying the dimensions from an existing array and allocating space for the array elements to be calculated.  I tried:
> 
>     result : APL_Data_Array_Pointer := new APL_Data_Array'(
>                g_shape => new Dimensions'(right.g_shape.all),
>                g_data  => new Elements(right.g_data.all'Range)
>              );
> 
> 
> This gives me an error message:
> 
> apl_data_arrays.adb:180:36: type of aggregate has private ancestor "Controlled"
> apl_data_arrays.adb:180:36: must use extension aggregate
> 
The Aggregate syntax would normally be:
             result : APL_Data_Array_Pointer := new APL_Data_Array'(
               APL_Arrays.APL_Array(right) with  -- This line changed!
               g_data  => new Elements(right.g_data.all'Range)
             ); 

BUT DO NOT DO THIS because g_shape is an access type and you almost
certainly don't want to copy that directly, which is what it will
do UNLESS you create an adjust procedure for it that correctly
handles doing a deep copy of the pointed to object.

> 
> But when I try:
> 
>     result : APL_Data_Array_Pointer := new APL_Data_Array'(
>                APL_Array'(new Dimensions'(right.g_shape.all)) with
>                g_data  => new Elements(right.g_data.all'Range)
>              );
> 
> 
> I get another error message:
> 
> apl_data_arrays.adb:180:48: no selector "g_shape" for type "APL_Data_Array" defined at apl_data_arrays.ads:46
> 
> This puzzles me since g_shape is a member of the parent class and should be visible to the child class.  I don't see how to fix the problem.
> 

The g_shape parameter is "private", so it is not visible to the child class 
as is, however if you make APL_Data_Arrays a child package of APL_Arrays
it can then see into the parent type:

   generic
      type Element is private;

   package APL_Arrays.APL_Data_Arrays is 
      -- all your stuff
   end APL_Arrays.APL_Data_Arrays;

Child packages can see into the private sections of their parent packages.
However my first comment can solve your problem directly if you add a 
correct Adjust procedure for the APL_Array type.

  reply	other threads:[~2020-01-27  1:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-27  0:22 Having problems instantiating a child class with an extension aggregate b.mcguinness747
2020-01-27  1:04 ` Jere [this message]
2020-01-27 17:47 ` Simon Wright
2020-01-27 23:08 ` b.mcguinness747
replies disabled

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