comp.lang.ada
 help / color / mirror / Atom feed
* Re: Heterogenous_Array_Test
       [not found] <Pine.LNX.4.33.0108141912510.32177-100000@lagoa.niaad.liacc.up.pt>
@ 2001-08-14 19:40 ` David C. Hoos
  2001-08-14 20:33   ` Heterogenous_Array_Test tmoran
  2001-08-16 19:06 ` Heterogenous_Array_Test M. A. Alves
  1 sibling, 1 reply; 5+ messages in thread
From: David C. Hoos @ 2001-08-14 19:40 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: maa

The following code compiles and executes correctly.

with Ada.Tags;
with Ada.Text_IO;
procedure Heterogeneous_Array_Test is

   type Root is tagged record
      Root_Component: Integer := 0;
   end record;

   type Child is new Root with record
      Child_Component: Integer := 0;
   end record;

   type Class_Ptr is access Root'Class;

   Item: array(1 .. 2) of Class_Ptr :=
     (1 => new Root,
      2 => new Child);

begin

   Item(1).Root_Component := 11;

   Item(2).Root_Component := 21;

   Child (Item(2).all).Child_Component := 22;

   for I in Item'Range loop
      Ada.Text_IO.Put_Line
        ("Type of Item (" & Positive'Image (I) & " ) is " &
         Ada.Tags.External_Tag (Item(i)'Tag));
   end loop;

end;

However, it does seem at first glance that the assignment to
Item (2),Child_Component should not have been ambiguous.

Perhaps one of the "language lawyers" reading this newsgroup
will explain it to us.

----- Original Message -----
From: "M. A. Alves" <maa@liacc.up.pt>
To: <comp.lang.ada@ada.eu.org>
Sent: Tuesday, August 14, 2001 3:12 PM
Subject: Heterogenous_Array_Test


> I am resuming an old thread (June 2001).  I was the original poster.
> Here I repost the problem with a summary review of the answers (to consult
> the entire old thread you may go to "http://groups.google.com/groups",
> search for "comp.lang.ada", then for "Heterogenous_Array_Test").  I do
> this because *** I found the answers inconclusive *** (I do this only now
> because of a conjugation of factors: I took some time trying to solve the
> problem by myself; I was changing offices then and my mail reading was out
> of order; the recent thread "Ambiguous reference - What is wrong with
> this?" seems to deal with similar issues).
>
> The problem is implementing an heterogenous array.  Candidate code:
>
>
> procedure Heterogenous_Array_Test is
>
>   type Root is tagged record
>     Root_Component: Integer := 0;
>   end record;
>
>   type Child is new Root with record
>     Child_Component: Integer := 0;
>   end record;
>
>   type Class_Ptr is access Root'Class;
>
>   Item: array(1 .. 2) of Class_Ptr := (
>     1 => new Root,
>     2 => new Child);
>
> begin
>
>   Item(1).Root_Component := 11;
>
>   Item(2).Root_Component := 21;
>
>   Item(2).Child_Component := 22;
>     -- error: no selector "Child_Component" for type "Root'Class"
>
> end;
>
>
> Ehud Lamm confirmed that "the approach is ok". The code has an error near
> the end.  Originally I asked for explanation of the error.  That was done.
> But no working solution was given.  Ted Dennison suggested changing the
> offending line to
>
>   Child'(Item(2).all).Child_Component := 22;
>
> That is intuitive (as was the original to me), but also does not compile.
> Ehud Lamm posted an example for derivation and dispatching, but NOT for
> the original problem of an heterogenous array.
>
> Thanks all very much,
>
> --
>    ,
>  M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
>  A M A D O   Rua Campo Alegre, 823         fax 351+226003654
>  A L V E S   P-4150 PORTO, Portugal        mob 351+939354002
>
>
>
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>




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

* Heterogenous_Array_Test
@ 2001-08-14 20:12 M. A. Alves
  2001-08-14 20:43 ` Heterogenous_Array_Test James Rogers
  0 siblings, 1 reply; 5+ messages in thread
From: M. A. Alves @ 2001-08-14 20:12 UTC (permalink / raw)
  To: comp.lang.ada

I am resuming an old thread (June 2001).  I was the original poster.
Here I repost the problem with a summary review of the answers (to consult
the entire old thread you may go to "http://groups.google.com/groups",
search for "comp.lang.ada", then for "Heterogenous_Array_Test").  I do
this because *** I found the answers inconclusive *** (I do this only now
because of a conjugation of factors: I took some time trying to solve the
problem by myself; I was changing offices then and my mail reading was out
of order; the recent thread "Ambiguous reference - What is wrong with
this?" seems to deal with similar issues).

The problem is implementing an heterogenous array.  Candidate code:


procedure Heterogenous_Array_Test is

  type Root is tagged record
    Root_Component: Integer := 0;
  end record;

  type Child is new Root with record
    Child_Component: Integer := 0;
  end record;

  type Class_Ptr is access Root'Class;

  Item: array(1 .. 2) of Class_Ptr := (
    1 => new Root,
    2 => new Child);

begin

  Item(1).Root_Component := 11;

  Item(2).Root_Component := 21;

  Item(2).Child_Component := 22;
    -- error: no selector "Child_Component" for type "Root'Class"

end;


Ehud Lamm confirmed that "the approach is ok". The code has an error near
the end.  Originally I asked for explanation of the error.  That was done.
But no working solution was given.  Ted Dennison suggested changing the
offending line to

  Child'(Item(2).all).Child_Component := 22;

That is intuitive (as was the original to me), but also does not compile.
Ehud Lamm posted an example for derivation and dispatching, but NOT for
the original problem of an heterogenous array.

Thanks all very much,

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002






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

* Re: Heterogenous_Array_Test
  2001-08-14 19:40 ` Heterogenous_Array_Test David C. Hoos
@ 2001-08-14 20:33   ` tmoran
  0 siblings, 0 replies; 5+ messages in thread
From: tmoran @ 2001-08-14 20:33 UTC (permalink / raw)


My understanding is that Child'(...) in
   Child'(Item(2).all).Child_Component := 22;
means "BTW, compiler, if you're confused, Item(2).all is a Child"
whereas Child(...) in
   Child (Item(2).all).Child_Component := 22;
means "compiler, convert Item(2).all to a Child (leaving it alone if
it's already one)"
so
   Child (Item(2).all).Child_Component := 22;
   Item(2) := new Root;
   Child (Item(2).all).Child_Component := 33;  -- should raise exception



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

* Re: Heterogenous_Array_Test
  2001-08-14 20:12 Heterogenous_Array_Test M. A. Alves
@ 2001-08-14 20:43 ` James Rogers
  0 siblings, 0 replies; 5+ messages in thread
From: James Rogers @ 2001-08-14 20:43 UTC (permalink / raw)


"M. A. Alves" wrote:
> 
> I am resuming an old thread (June 2001).  I was the original poster.
> Here I repost the problem with a summary review of the answers (to consult
> the entire old thread you may go to "http://groups.google.com/groups",
> search for "comp.lang.ada", then for "Heterogenous_Array_Test").  I do
> this because *** I found the answers inconclusive *** (I do this only now
> because of a conjugation of factors: I took some time trying to solve the
> problem by myself; I was changing offices then and my mail reading was out
> of order; the recent thread "Ambiguous reference - What is wrong with
> this?" seems to deal with similar issues).
> 

Note that you can create a heterogeneous array using access to
base'class.
The problem is that this creates a limited view of the array elements.
They are all viewed as being type Root. You must coerce the child
component back into its actual type to see the additional fields.

A heterogeneous array of this sort is most easily used when calling
overridden subprograms. This allows the subprogram calls to be
dynamically dispatched.

procedure Heterogenous_Array_Test is
  type Root is tagged record
    Root_Component: Integer := 0;
  end record;

  type Child is new Root with record
    Child_Component: Integer := 0;
  end record;

  type Class_Ptr is access Root'Class;

  Item: array(1 .. 2) of Class_Ptr := (
    1 => new Root,
    2 => new Child);

begin

  Item(1).Root_Component := 11;

  Item(2).Root_Component := 21;

  child(Item(2).all).Child_Component := 22;    
  -- error fixed
 
end;

Jim Rogers
Colorado Springs, Colorado USA



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

* Re: Heterogenous_Array_Test
       [not found] <Pine.LNX.4.33.0108141912510.32177-100000@lagoa.niaad.liacc.up.pt>
  2001-08-14 19:40 ` Heterogenous_Array_Test David C. Hoos
@ 2001-08-16 19:06 ` M. A. Alves
  1 sibling, 0 replies; 5+ messages in thread
From: M. A. Alves @ 2001-08-16 19:06 UTC (permalink / raw)
  To: comp.lang.ada

So, the specific (coding) problem is fixed with type conversion:

  Child(Item(2).all).Child_Component := 22;

However, as was pointed out, that is not fully satisfactory an idiom (for
heterogenous arrays).  The really fine aproach is dispatching.  So I tried
to define dispatching procedures in the declarative part of the test
procedure---and it did not work!  Why?  Because

  *** dispatching subprograms must be defined in a package spec ***

I got this tiny but essential piece of knowledge as a warning from the
compiler (GNAT 3.13p) not before a good number of tries, so I thought I
should share it with you good folks.

_Now_ my original general problem (implementing an heterogenous array) is
solved.

Thanks all,

-- 
   ,
 M A R I O   data miner, LIACC, room 221   tel 351+226078830, ext 121
 A M A D O   Rua Campo Alegre, 823         fax 351+226003654
 A L V E S   P-4150 PORTO, Portugal        mob 351+939354002






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

end of thread, other threads:[~2001-08-16 19:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-14 20:12 Heterogenous_Array_Test M. A. Alves
2001-08-14 20:43 ` Heterogenous_Array_Test James Rogers
     [not found] <Pine.LNX.4.33.0108141912510.32177-100000@lagoa.niaad.liacc.up.pt>
2001-08-14 19:40 ` Heterogenous_Array_Test David C. Hoos
2001-08-14 20:33   ` Heterogenous_Array_Test tmoran
2001-08-16 19:06 ` Heterogenous_Array_Test M. A. Alves

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