comp.lang.ada
 help / color / mirror / Atom feed
* Re: Heterogenous_Array_Test: please explain error
  2001-06-12 20:32 Heterogenous_Array_Test: please explain error M. A. Alves
@ 2001-06-12 19:44 ` Ehud Lamm
  2001-06-12 19:50 ` Ted Dennison
  1 sibling, 0 replies; 8+ messages in thread
From: Ehud Lamm @ 2001-06-12 19:44 UTC (permalink / raw)


The approach is ok. The problem is that you must tell the compiler that the
class-wide pointer points to an object of a type that has the spefici
component you ask for: since this component is not part of the ancestor
type, and tus isn't part of the 'class type.
Notice that since Ada tries to check as much as possible before runtime it
falgs such a use as problematic, unless you explicitly say that the object
is of the specific type (see below). Usually such conversions/castings  are
not good oop style (use dispatching instrad).

Below is a simple example I use to cover these issues, the operations in the
package are trivial put statements  so I am not including them here.

HTH

package try_tagged_pack is

-- We define an abstract base "class" shape
type shape is abstract tagged null record;
-- and an abstract procedure that will be dispatcahble
procedure draw(s: in out Shape) is abstract;
procedure any_shape(s:in out shape'class);
procedure any2(s: in out shape);

type square is new Shape with
   record
      side:integer;
   end record;
procedure draw(s: in out square);


type circle is new shape with
   record
      radius:integer;
   end record;
procedure draw(c:in out circle);



 type ptr is access shape'class;
   type ptr2 is access circle;
   p:ptr;
   q:ptr2;
   s:square;
   c:circle;
  -- We can not declare variables of an abstract type.
  -- sh:shape;

  -- Nor can we declare a varible of a classwide type (it is unconstraind)
  -- unless we use initialization to a specific type (like s:string:="ehud")
  --sh:shape'class;
begin

   draw(s);
   c.radius:=5;
   p:= new circle'(radius=>5);
   q:=new circle;
   q.radius:=5;
   -- p is not constrained enough
  --- p.radius:=5;
  -- one possible solution (expalin why problematic!):
  circle(p.all).radius:=5;

   draw(p.all);
   -- We invoke any_shape on some variables of different types.
   any_shape(s);
   any_shape(c);
   any_shape(p.all);
   -- Comapre any_shape and any2
   any2(s);
   any2(c);
   any2(p.all);
end;


--
Ehud Lamm   mslamm@mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm <==  Me!








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

* Re: Heterogenous_Array_Test: please explain error
  2001-06-12 20:32 Heterogenous_Array_Test: please explain error M. A. Alves
  2001-06-12 19:44 ` Ehud Lamm
@ 2001-06-12 19:50 ` Ted Dennison
  2001-06-13  9:52   ` Lutz Donnerhacke
  1 sibling, 1 reply; 8+ messages in thread
From: Ted Dennison @ 2001-06-12 19:50 UTC (permalink / raw)


In article <mailman.992374395.5423.comp.lang.ada@ada.eu.org>, M. A. Alves
says...
>
>Isn't this the 'standard' way of implementing an heterogenous array?
>Why is it not working?  Please explain the compiler's error on the last
>statement.  Thanks.
>  Item(2).Child_Component := 22;
>    -- error: no selector "Child_Component" for type "Root'Class"

The problem is pretty much what the compiler says it is. The type of elements in
your Item array is "Root'Class". That type doesn't have any components named
"Child_Component". If you want to access that component, you have to tell the
compiler that the type is really "Child" (which it will verify at runtime just
to be sure). I think something like the following will work, though I haven't
compiled it:

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

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Heterogenous_Array_Test: please explain error
@ 2001-06-12 20:32 M. A. Alves
  2001-06-12 19:44 ` Ehud Lamm
  2001-06-12 19:50 ` Ted Dennison
  0 siblings, 2 replies; 8+ messages in thread
From: M. A. Alves @ 2001-06-12 20:32 UTC (permalink / raw)
  To: comp.lang.ada

Isn't this the 'standard' way of implementing an heterogenous array?
Why is it not working?  Please explain the compiler's error on the last
statement.  Thanks.

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;

-- 
   ,
 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] 8+ messages in thread

* Re: Heterogenous_Array_Test: please explain error
  2001-06-12 19:50 ` Ted Dennison
@ 2001-06-13  9:52   ` Lutz Donnerhacke
  2001-06-13 14:42     ` Ted Dennison
  0 siblings, 1 reply; 8+ messages in thread
From: Lutz Donnerhacke @ 2001-06-13  9:52 UTC (permalink / raw)


* Ted Dennison wrote:
>"Child_Component". If you want to access that component, you have to tell the
>compiler that the type is really "Child" (which it will verify at runtime just
>to be sure). I think something like the following will work, though I haven't
>compiled it:
>
>Child'(Item(2).all).Child_Component := 22;

Does this work on a copy of Child initialized with Item(2).all?

I thought typecasts match the pattern "Typ(Expression)" not
"Typ'(Expression)", but i may fail.



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

* Re: Heterogenous_Array_Test: please explain error
  2001-06-13  9:52   ` Lutz Donnerhacke
@ 2001-06-13 14:42     ` Ted Dennison
  2001-06-13 14:48       ` Ehud Lamm
  0 siblings, 1 reply; 8+ messages in thread
From: Ted Dennison @ 2001-06-13 14:42 UTC (permalink / raw)


In article <slrn9iedo6.hl.lutz@taranis.iks-jena.de>, Lutz Donnerhacke says...
>
>* Ted Dennison wrote:
>>"Child_Component". If you want to access that component, you have to tell the
>>compiler that the type is really "Child" (which it will verify at runtime just
>>to be sure). I think something like the following will work, though I haven't
>>compiled it:
>>
>>Child'(Item(2).all).Child_Component := 22;
>
>Does this work on a copy of Child initialized with Item(2).all?
>
>I thought typecasts match the pattern "Typ(Expression)" not
>"Typ'(Expression)", but i may fail.

The form without the tick is a conversion, and constraint checks (along with any
other appropriate checks) will be performed. The form with the tick (a
"qualified expression") is more like a clarification, or a hint to the compiler
which might have otherwise been confused about what type is being used. For that
reason, I'm not sure it will work here. You might need to remove the tick and
try the conversion.

Note that this is a simple explanation, not a fully accurate one. To get that,
you'd need to consult the LRM.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: Heterogenous_Array_Test: please explain error
  2001-06-13 14:42     ` Ted Dennison
@ 2001-06-13 14:48       ` Ehud Lamm
  2001-06-13 15:40         ` Ted Dennison
  0 siblings, 1 reply; 8+ messages in thread
From: Ehud Lamm @ 2001-06-13 14:48 UTC (permalink / raw)


> The form without the tick is a conversion, and constraint checks (along
with any
> other appropriate checks) will be performed. The form with the tick (a
> "qualified expression") is more like a clarification, or a hint to the
compiler
> which might have otherwise been confused about what type is being used.
For that
> reason, I'm not sure it will work here. You might need to remove the tick
and
> try the conversion.
>

In the example I posted as an answer to the original quation, I use a
conversion. A qualified expression doesn't compile (using Gnat 3.13) (And in
any case, I'd want the checks done  if at all possible)


--
Ehud Lamm   mslamm@mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm <==  Me!








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

* Re: Heterogenous_Array_Test: please explain error
  2001-06-13 14:48       ` Ehud Lamm
@ 2001-06-13 15:40         ` Ted Dennison
  2001-06-13 17:10           ` Ehud Lamm
  0 siblings, 1 reply; 8+ messages in thread
From: Ted Dennison @ 2001-06-13 15:40 UTC (permalink / raw)


In article <9g7uub$70u$1@news.huji.ac.il>, Ehud Lamm says...
>In the example I posted as an answer to the original quation, I use a
>conversion. A qualified expression doesn't compile (using Gnat 3.13) (And in
>any case, I'd want the checks done  if at all possible)

According to the LRM a qualified expression will do a subtype constraint check,
but in this case I'm pretty sure you are right. We are talking about 2 *derived*
types here, and the compiler is already *sure* its one of them, so its not a
matter of simple clarification.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: Heterogenous_Array_Test: please explain error
  2001-06-13 15:40         ` Ted Dennison
@ 2001-06-13 17:10           ` Ehud Lamm
  0 siblings, 0 replies; 8+ messages in thread
From: Ehud Lamm @ 2001-06-13 17:10 UTC (permalink / raw)




> According to the LRM a qualified expression will do a subtype constraint
check,
> but in this case I'm pretty sure you are right. We are talking about 2
*derived*
> types here, and the compiler is already *sure* its one of them, so its not
a
> matter of simple clarification.
>
In fact I was thinking to myself that this is one subtle issue where the LRM
definitions are not easily understandable. Skiming the LRM on qualified
expression I wasn't sure if this should work. This is one palce where I
agree with decision not to use LRM references in compiler error messages...
(contrary to my usuall stand on the LRM references issue).


--
Ehud Lamm   mslamm@mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm <==  Me!











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

end of thread, other threads:[~2001-06-13 17:10 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-12 20:32 Heterogenous_Array_Test: please explain error M. A. Alves
2001-06-12 19:44 ` Ehud Lamm
2001-06-12 19:50 ` Ted Dennison
2001-06-13  9:52   ` Lutz Donnerhacke
2001-06-13 14:42     ` Ted Dennison
2001-06-13 14:48       ` Ehud Lamm
2001-06-13 15:40         ` Ted Dennison
2001-06-13 17:10           ` Ehud Lamm

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