comp.lang.ada
 help / color / mirror / Atom feed
* Q: Hiding the structure of a tagged type using containers
@ 2006-05-09  1:52 brian.b.mcguinness
  2006-05-09  6:43 ` Stephen Leake
  2006-05-09 14:47 ` Matthew Heaney
  0 siblings, 2 replies; 6+ messages in thread
From: brian.b.mcguinness @ 2006-05-09  1:52 UTC (permalink / raw)


I am trying to create a tree of object classes to represent arrays.  I
want to hide the internal representation of the arrays in the private
sections of the packages for the tagged types.  However, I am having
trouble figuring out how to do this.  For example, I have an abstract
base
class called APL_Array, and I am trying to create a child class called
APL_Array_Dimensioned that contains a list of dimensions (which may
change
over time, say if someone concatenates a row or column to a matrix).
This
class can provide various structural operations that only affect the
list
of dimensions.  Then I can descend a generic child array class from
this
to hold a list of array elements of any desired data type.

I tried this:

1
--------------------------------------------------------------------------------
2  -- Declaration of an array with a list of dimensions
3  --
4  -- Brian B. McGuinness     May, 2006
5
--------------------------------------------------------------------------------
6  with Ada.Containers.Vectors;
7
8  package APL.Arrays.Dimensioned is
9
10    type APL_Shape is private;
11
12    type APL_Array_Dimensioned is new APL_Array with private;
13
14    -- Get the total number of elements
15    overriding function Length (A : APL_Array_Dimensioned) return
Dimension;
16
17    -- Get the number of dimensions
18    function Rank (A : APL_Array_Dimensioned) return Dimension_Count;
19
20    -- Get the current shape of the array
21    procedure Shape (A : in APL_Array_Dimensioned; Dimensions : out
APL_Shape);
22
23    -- Set the shape of the array
24    procedure Replace_Shape (A : in out APL_Array_Dimensioned;
Dimensions : in APL_Shape);
25
26  private
27
28    package APL_Dimension_List is new Ada.Containers.Vectors (
29      Index_Type   => Dimension_Index,
30      Element_Type => Dimension
31    );
32
33    type APL_Shape is new APL_Dimension_List.Vector with null record;
34
35    type APL_Array_Dimensioned is new APL_Array with record
36      Dimensions : APL_Shape;
37    end record;
38
39  end APL.Arrays.Dimensioned;

but when I try to compile it, I get:

# gnatmake -gnat05 apl-arrays-dimensioned.adb
gcc -c -gnat05 apl-arrays-dimensioned.adb
apl-arrays-dimensioned.ads:21:13: operation can be dispatching in only
one type
apl-arrays-dimensioned.ads:24:13: operation can be dispatching in only
one type
apl-arrays-dimensioned.ads:33:08: type must be declared abstract or
"TO_VECTOR" overridden
apl-arrays-dimensioned.ads:33:08: "TO_VECTOR" has been inherited at
line 33
apl-arrays-dimensioned.ads:33:08: "TO_VECTOR" has been inherited from
subprogram at a-convec.ads:63, instance at line 28
gnatmake: "apl-arrays-dimensioned.adb" compilation error
#

Using APL_Dimension_List.Vector might work, but I am trying to
rename this to APL_Shape in order to hide the ".Vector" part
and make the dimension list look like a simple data type. I
tried using "type APL_Shape renames APL_Dimension_List.Vector"
but that doesn't work, either.

If the Ada 2005 RM provides information on how to do this,
I have been unable to find it.

I would appreciate any help that you can provide.

--- Brian




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

* Re: Q: Hiding the structure of a tagged type using containers
  2006-05-09  1:52 Q: Hiding the structure of a tagged type using containers brian.b.mcguinness
@ 2006-05-09  6:43 ` Stephen Leake
  2006-05-09 14:52   ` Matthew Heaney
  2006-05-09 14:47 ` Matthew Heaney
  1 sibling, 1 reply; 6+ messages in thread
From: Stephen Leake @ 2006-05-09  6:43 UTC (permalink / raw)


brian.b.mcguinness@lmco.com writes:

> I am trying to create a tree of object classes to represent arrays.
> I want to hide the internal representation of the arrays in the
> private sections of the packages for the tagged types. However, I am
> having trouble figuring out how to do this. For example, I have an
> abstract base class called APL_Array, and I am trying to create a
> child class called APL_Array_Dimensioned that contains a list of
> dimensions (which may change over time, say if someone concatenates
> a row or column to a matrix).

I used to use APL! fun language :).

> <snip code> 
>
> but when I try to compile it, I get:
>
> # gnatmake -gnat05 apl-arrays-dimensioned.adb
> gcc -c -gnat05 apl-arrays-dimensioned.adb
> apl-arrays-dimensioned.ads:21:13: operation can be dispatching in only
> one type

The general solution to this error is to make one of the arguments
class-wide; in this case:

procedure Shape
   (A : in APL_Array_Dimensioned; Dimensions : out APL_Shape'class);

or

procedure Shape
   (A : in APL_Array_Dimensioned'class; Dimensions : out APL_Shape);

If children of APL_Array_Dimensioned don't need to override Shape, the
second declaration is more appropriate.

Another solution is to use only one tagged type. Apparently
APL_Dimension_List.Vector is visibly tagged; perhaps it does not need
to be?

-- 
-- Stephe



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

* Re: Q: Hiding the structure of a tagged type using containers
  2006-05-09  1:52 Q: Hiding the structure of a tagged type using containers brian.b.mcguinness
  2006-05-09  6:43 ` Stephen Leake
@ 2006-05-09 14:47 ` Matthew Heaney
  2006-05-09 14:55   ` brian.b.mcguinness
  1 sibling, 1 reply; 6+ messages in thread
From: Matthew Heaney @ 2006-05-09 14:47 UTC (permalink / raw)



brian.b.mcguinness@lmco.com wrote:
> I am trying to create a tree of object classes to represent arrays.  I
> want to hide the internal representation of the arrays in the private
> sections of the packages for the tagged types.

Yes, that's a good idea.


> However, I am having
> trouble figuring out how to do this.
> I tried this:
>
> 1
> 6  with Ada.Containers.Vectors;
> 7
> 8  package APL.Arrays.Dimensioned is
> 9
> 10    type APL_Shape is private;
> 11
> 26  private
> 27
> 28    package APL_Dimension_List is new Ada.Containers.Vectors (
> 29      Index_Type   => Dimension_Index,
> 30      Element_Type => Dimension
> 31    );
> 32
> 33    type APL_Shape is new APL_Dimension_List.Vector with null record;

Why is this a derivation?  Just say:

  type APL_Shape is record
     V : APL_Dimension_List.Vector;
  end record;



> 35    type APL_Array_Dimensioned is new APL_Array with record
> 36      Dimensions : APL_Shape;
> 37    end record;
> 38
> 39  end APL.Arrays.Dimensioned;
>
>
> Using APL_Dimension_List.Vector might work, but I am trying to
> rename this to APL_Shape in order to hide the ".Vector" part
> and make the dimension list look like a simple data type.

Fine, just implement APL_Shape as a non-tagged record with a vector
component.




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

* Re: Q: Hiding the structure of a tagged type using containers
  2006-05-09  6:43 ` Stephen Leake
@ 2006-05-09 14:52   ` Matthew Heaney
  2006-05-10 11:50     ` Craig Carey
  0 siblings, 1 reply; 6+ messages in thread
From: Matthew Heaney @ 2006-05-09 14:52 UTC (permalink / raw)



Stephen Leake wrote:
> brian.b.mcguinness@lmco.com writes:
>
>
> The general solution to this error is to make one of the arguments
> class-wide; in this case:
>
> procedure Shape
>    (A : in APL_Array_Dimensioned; Dimensions : out APL_Shape'class);

This won't work, since the APL_Shape isn't tagged at the point where
Shape is declared.


> Another solution is to use only one tagged type. Apparently
> APL_Dimension_List.Vector is visibly tagged; perhaps it does not need
> to be?

We debated this during the design of the container library, and
concluded that we might as well make the type tagged, since the most
likely implementation would be that container types privately derive
from Controlled.  Making the container types publicly tagged then gives
you other benefits such as as implicit aliasing of subprogram
parameters and distinguished-receiver syntax.

The solution in this case is to implement the full view of type
APL_Shape as a non-tagged record, with a vector component.

-Matt




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

* Re: Q: Hiding the structure of a tagged type using containers
  2006-05-09 14:47 ` Matthew Heaney
@ 2006-05-09 14:55   ` brian.b.mcguinness
  0 siblings, 0 replies; 6+ messages in thread
From: brian.b.mcguinness @ 2006-05-09 14:55 UTC (permalink / raw)


Ok, thanks to both of you for the help.

--- Brian




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

* Re: Q: Hiding the structure of a tagged type using containers
  2006-05-09 14:52   ` Matthew Heaney
@ 2006-05-10 11:50     ` Craig Carey
  0 siblings, 0 replies; 6+ messages in thread
From: Craig Carey @ 2006-05-10 11:50 UTC (permalink / raw)


On 9 May 2006 07:52:00 -0700, "Matthew Heaney" wrote:
>Stephen Leake wrote:
>> brian.b.mcguinness@lmco.com writes:
...
>We debated this during the design of the container library, and

The debating forum got boycotted ... but too late, IBM France and
Jim Moore and etc. and whomever were taking months to get discussion
e-mails if at all.

>concluded that we might as well make the type tagged, since the most
>likely implementation

Rather than, say. actual implementations that browsers could promptly
 access. There is one here:

http://terratope.tigris.org/source/browse/terratope/trunk/cy/cc/tope/tp-m1.ads

Were's you the man talking about the politics of US gangsters?.


> would be that container types privately derive
>from Controlled.

Would be that tagged were tried and after some effort, finally 
abandonded with a finding that they seem useless

> Making the container types publicly tagged then gives
>you other benefits

Such as having nearly zero heavyweight users...

> such as as implicit aliasing of subprogram
>parameters and distinguished-receiver syntax.
>

That is a major matter but a way to fix it is have absolutely
no reference manual guarantee of aliasing and just note that the
GNAT compiler uses by-references pointers anyway. Did you or Taft
of arg@ada-auth.org really get to discover what the problems are
with an aliased type, or the adding of an "aliased" keyword to
the parameter list ?.

>The solution in this case is to implement the full view of type
>APL_Shape as a non-tagged record, with a vector component.

My solution is to use enumerated types in records that did not
implement a variant, but merely differentiated between different
heaps for different CPUs, and use unchecked pointer conversion.

In my opinion tagged types (ie. O.O.) are seriously outmatered
by the competition of nested records, and of course there would
be some record flattening syntax (without and any hidden tag
discriminant) and beyond all reasonable doubt, Dewar's Ada
still does not have such.

Craig Carey



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

end of thread, other threads:[~2006-05-10 11:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-09  1:52 Q: Hiding the structure of a tagged type using containers brian.b.mcguinness
2006-05-09  6:43 ` Stephen Leake
2006-05-09 14:52   ` Matthew Heaney
2006-05-10 11:50     ` Craig Carey
2006-05-09 14:47 ` Matthew Heaney
2006-05-09 14:55   ` brian.b.mcguinness

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