comp.lang.ada
 help / color / mirror / Atom feed
* Having problems instantiating a child class with an extension aggregate
@ 2020-01-27  0:22 b.mcguinness747
  2020-01-27  1:04 ` Jere
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: b.mcguinness747 @ 2020-01-27  0:22 UTC (permalink / raw)



I have three packages:

--------------------------------------------------------------------------------
-- Basic data types used by other packages
--------------------------------------------------------------------------------
package APL_Types is
  ...

  type    Int64    is       range -9223372036854775808..9223372036854775807;
  subtype Int32    is Int64 range -2147483648..2147483647;
  subtype Int16    is Int64 range -32768..32767;

  subtype APL_Integer is Int64;

  ...

  type    Array_Index is range 0..APL_Integer'Last;

  type    Index_List         is array(Array_Index range <>) of Array_Index;
  type    Index_List_Pointer is access Index_List;

  procedure Free_Index_List is new Ada.Unchecked_Deallocation (Index_List, Index_List_Pointer);

  subtype Dimensions         is Index_List;
  type    Dimensions_Pointer is access Dimensions;

  procedure Free_Dimensions is new Ada.Unchecked_Deallocation (Dimensions, Dimensions_Pointer);

  ...
end APL_Types;


--------------------------------------------------------------------------------
-- This holds the dimensions of the array, which can change over time
--
-- The array elements are handled by child classes since they might either be
-- stored in the array or calculated when required.
--------------------------------------------------------------------------------
with Ada.Finalization;
with APL_Types;

package APL_Arrays is
  use APL_Types;

  type APL_Array         is new Ada.Finalization.Controlled with private;
  type APL_Array_Pointer is access APL_Array;

  function  Effective_Rank (array_rank, function_rank : Array_Index) return Array_Index;

  procedure Finalize (this : in out APL_Array);

  function  Length         (this : in APL_Array; axis : Array_Index) return Array_Index;
  function  Rank           (this : in APL_Array) return Array_Index;
  function  Raveled_Length (this : in APL_Array) return Array_Index;

  function  Raveled_Shape (
    shape         : Dimensions; 
    function_rank : Array_Index; 
    to_table      : in Boolean := false
  ) return Dimensions_Pointer;

  function  Same_Shape  (this, other : in APL_Array) return Boolean;

private
  type APL_Array is new Ada.Finalization.Controlled with record
    g_shape : Dimensions_Pointer;
  end record;
end APL_Arrays;


--------------------------------------------------------------------------------
-- Arrays that store each array element
--------------------------------------------------------------------------------
with Ada.Unchecked_Deallocation;
with APL_Arrays, APL_Types;

generic
  type Element is private;

package APL_Data_Arrays is
  use APL_Types;

  type APL_Data_Array          is new APL_Arrays.APL_Array with private;
  type APL_Data_Array_Pointer  is access APL_Data_Array;

  type Scalar_Monadic_Function is access function (right : Element) return Element;
  type Scalar_Dyadic_Function  is access function (left, right : Element) return Element;

  function Apply_Scalar_Dyadic (
    left,  right : in APL_Data_Array;      -- The arrays to be operated on
    lrank, rrank : Array_Index;            -- The left and right function rank
    fn           : Scalar_Dyadic_Function  -- Function to apply to the arrays
  ) return APL_Data_Array_Pointer;

  function Apply_Scalar_Monadic (
    right : in APL_Data_Array;       -- The array to be operated on
    fn    : Scalar_Monadic_Function  -- The function to apply to the array
  ) return APL_Data_Array_Pointer;

  procedure Finalize (this : in out APL_Data_Array);

  overriding function Raveled_Length (this : in APL_Data_Array) return Array_Index;

private
  use APL_Types;

  type Elements is array(Array_Index range <>) of Element;
  type Elements_Pointer is access Elements;

  procedure Free_Elements is new Ada.Unchecked_Deallocation (Elements, Elements_Pointer);

  type APL_Data_Array is new APL_Arrays.APL_Array with record
    g_data : Elements_Pointer;
  end record;
end APL_Data_Arrays;



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


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.

I would appreciate help.

--- Brian McGuinness

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Having problems instantiating a child class with an extension aggregate
  2020-01-27  0:22 Having problems instantiating a child class with an extension aggregate b.mcguinness747
@ 2020-01-27  1:04 ` Jere
  2020-01-27 17:47 ` Simon Wright
  2020-01-27 23:08 ` b.mcguinness747
  2 siblings, 0 replies; 4+ messages in thread
From: Jere @ 2020-01-27  1:04 UTC (permalink / raw)


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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Having problems instantiating a child class with an extension aggregate
  2020-01-27  0:22 Having problems instantiating a child class with an extension aggregate b.mcguinness747
  2020-01-27  1:04 ` Jere
@ 2020-01-27 17:47 ` Simon Wright
  2020-01-27 23:08 ` b.mcguinness747
  2 siblings, 0 replies; 4+ messages in thread
From: Simon Wright @ 2020-01-27 17:47 UTC (permalink / raw)


b.mcguinness747@gmail.com writes:

>     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

You need to include the private ancestor:

  ... new APL_Data_Array'(Controlled with ...

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Having problems instantiating a child class with an extension aggregate
  2020-01-27  0:22 Having problems instantiating a child class with an extension aggregate b.mcguinness747
  2020-01-27  1:04 ` Jere
  2020-01-27 17:47 ` Simon Wright
@ 2020-01-27 23:08 ` b.mcguinness747
  2 siblings, 0 replies; 4+ messages in thread
From: b.mcguinness747 @ 2020-01-27 23:08 UTC (permalink / raw)


Thanks very much for your help.  

I renamed the package to APL_Arrays.APL_Data_Arrays and changed the instantiation to

    result : APL_Data_Array_Pointer := new APL_Data_Array'(
               Ada.Finalization.Controlled with 
               g_shape => new Dimensions'(right.g_shape.all),
               g_data  => new Elements(right.g_data.all'Range)
             );

Now it compiles properly.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-01-27 23:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-27  0:22 Having problems instantiating a child class with an extension aggregate b.mcguinness747
2020-01-27  1:04 ` Jere
2020-01-27 17:47 ` Simon Wright
2020-01-27 23:08 ` b.mcguinness747

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