comp.lang.ada
 help / color / mirror / Atom feed
* Embedded hierarchy
@ 2000-12-18 12:36 Philippe Tarroux
  2000-12-22  7:11 ` mark_lundquist
  0 siblings, 1 reply; 2+ messages in thread
From: Philippe Tarroux @ 2000-12-18 12:36 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 3146 bytes --]

Hi,

I want to define two object hierarchies with one depending on the other: An
object type and a structure containing objects (an array of object or something
else. It doesn't matter).
The constraints are that some operations must be defined for all the extensions
of both classes (this is why both types must be tagged). I try to find a way to
implement this problem in such a way that major misuse errors will be detected
at compile time rather than at run time.

Concretely, an implementation could be:

The object class:

package Obj_Handler is

   type Obj is abstract tagged null record;

   function "+"(A, B : Obj) return Obj is abstract;

   type Obj_A is new Obj with private;

   type Obj_B is new Obj with private;


private
   function "+"(A, B : Obj_A) return Obj_A;
   function "+"(A, B : Obj_B) return Obj_B;

   type Obj_A is new Obj with null record;
   type Obj_B is new Obj with null record;

end Obj_Handler;


The map containing objects

with Obj_Handler;
use Obj_Handler;

package Map_Handler is

   type Map is abstract tagged null record;

   function Object(M : access Map; I : Integer) return Obj'Class is abstract;

   type Map_A is new Map with private;
   type Map_A_Ptr is access Map_A;

   function Object(M : access Map_A; I : Integer) return Obj'Class;

   type Map_B is new Map with private;
   type Map_B_Ptr is access Map_B;

   function Object(M : access Map_B; I : Integer) return Obj'Class;

private

   type ObjA_Tab is array(Integer range <>) of Obj_A;
   type ObjB_Tab is array(Integer range <>) of Obj_B;

   type Map_A is new Map with record
      Object : ObjA_Tab(1..100);
   end record;

   type Map_B is new Map with record
      Object : ObjB_Tab(1..100);
   end record;

end Map_Handler;


and a test program:

with Obj_Handler;
with Map_Handler;

use Obj_Handler;
use Map_Handler;

procedure Test_Obj is

   A, B : Map_A_Ptr;
   C : Map_B_Ptr;

   X : Obj_A;

begin

   X := Obj_A(Object(A,10))+Obj_A(Object(C,3));

end Test_Obj;

which compiles, executes and produces a constraint error during the execution of
the assignment X := ...
The line is obviously faulty because Object(C,3) returns an Obj'Class which is
in fact an Obj_B and not an Obj_A. But this assignation cannot be
determined statically.

I tried different solutions but it seems that there is no way to avoid the
problem above.
I thought it was possible to define:

   function Object(M : access Map; I : Integer) return Obj is abstract;

and then overwrite this abstract function as:

   function Object(M : access Map_A; I : Integer) return Obj_A;

but it seems that this last definition does not overwrite the abstract function
correctly and I was unable to find a syntax that do it. Why does the compiler
not complain about this statement if there is no way to overwrite it?

Has somebody a suggestion to implement a better solution to this problem?

Many thanks


Development environment:  gnat 3.12p under Windows NT 4.0


--
Philippe Tarroux                        LIMSI-CNRS
email: tarroux@limsi.fr                 BP 133
Phone: +(33) 1 69 85 81 23              91470 Orsay cedex - France
Fax  : +(33) 1 69 85 80 20


[-- Attachment #2: Carte pour Philippe Tarroux --]
[-- Type: text/x-vcard, Size: 231 bytes --]

begin:vcard 
n:Tarroux;Philippe
tel;fax:+(33) 1 69 85 80 88
tel;work:+(33) 1 69 85 81 23
x-mozilla-html:TRUE
org:LIMSI-CNRS
adr:;;BP 133;Orsay;;91403;France
version:2.1
email;internet:tarroux@limsi.fr
fn:Philippe Tarroux
end:vcard

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

* Re: Embedded hierarchy
  2000-12-18 12:36 Embedded hierarchy Philippe Tarroux
@ 2000-12-22  7:11 ` mark_lundquist
  0 siblings, 0 replies; 2+ messages in thread
From: mark_lundquist @ 2000-12-22  7:11 UTC (permalink / raw)




Hello Philippe,

Well, for a problem like this the answer that suggests itself is to
make the "map class" generic on the "object class" type.

It looks like you know Ada95 pretty well, so I'm sure with that hint
you can figure out the details.

I think you'll find that this allows you to capture abstraction at the
level you want, and also provides the type safety you are looking for.

Have fun!
mark

Mark Lundquist
Ratoinal Software

In article <3A3E04B6.C50B6CDC@limsi.fr>,
  Philippe Tarroux <tarroux@limsi.fr> wrote:
> Il s'agit d'un message multivolet au format MIME.
> --------------F0270765BB3E3E0CA9C37A7A
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
>
> Hi,
>
> I want to define two object hierarchies with one depending on the
other: An
> object type and a structure containing objects (an array of object or
something
> else. It doesn't matter).
> The constraints are that some operations must be defined for all the
extensions
> of both classes (this is why both types must be tagged). I try to
find a way to
> implement this problem in such a way that major misuse errors will be
detected
> at compile time rather than at run time.
>
> Concretely, an implementation could be:
>
> The object class:
>
> package Obj_Handler is
>
>    type Obj is abstract tagged null record;
>
>    function "+"(A, B : Obj) return Obj is abstract;
>
>    type Obj_A is new Obj with private;
>
>    type Obj_B is new Obj with private;
>
> private
>    function "+"(A, B : Obj_A) return Obj_A;
>    function "+"(A, B : Obj_B) return Obj_B;
>
>    type Obj_A is new Obj with null record;
>    type Obj_B is new Obj with null record;
>
> end Obj_Handler;
>
> The map containing objects
>
> with Obj_Handler;
> use Obj_Handler;
>
> package Map_Handler is
>
>    type Map is abstract tagged null record;
>
>    function Object(M : access Map; I : Integer) return Obj'Class is
abstract;
>
>    type Map_A is new Map with private;
>    type Map_A_Ptr is access Map_A;
>
>    function Object(M : access Map_A; I : Integer) return Obj'Class;
>
>    type Map_B is new Map with private;
>    type Map_B_Ptr is access Map_B;
>
>    function Object(M : access Map_B; I : Integer) return Obj'Class;
>
> private
>
>    type ObjA_Tab is array(Integer range <>) of Obj_A;
>    type ObjB_Tab is array(Integer range <>) of Obj_B;
>
>    type Map_A is new Map with record
>       Object : ObjA_Tab(1..100);
>    end record;
>
>    type Map_B is new Map with record
>       Object : ObjB_Tab(1..100);
>    end record;
>
> end Map_Handler;
>
> and a test program:
>
> with Obj_Handler;
> with Map_Handler;
>
> use Obj_Handler;
> use Map_Handler;
>
> procedure Test_Obj is
>
>    A, B : Map_A_Ptr;
>    C : Map_B_Ptr;
>
>    X : Obj_A;
>
> begin
>
>    X := Obj_A(Object(A,10))+Obj_A(Object(C,3));
>
> end Test_Obj;
>
> which compiles, executes and produces a constraint error during the
execution of
> the assignment X := ...
> The line is obviously faulty because Object(C,3) returns an Obj'Class
which is
> in fact an Obj_B and not an Obj_A. But this assignation cannot be
> determined statically.
>
> I tried different solutions but it seems that there is no way to
avoid the
> problem above.
> I thought it was possible to define:
>
>    function Object(M : access Map; I : Integer) return Obj is
abstract;
>
> and then overwrite this abstract function as:
>
>    function Object(M : access Map_A; I : Integer) return Obj_A;
>
> but it seems that this last definition does not overwrite the
abstract function
> correctly and I was unable to find a syntax that do it. Why does the
compiler
> not complain about this statement if there is no way to overwrite it?
>
> Has somebody a suggestion to implement a better solution to this
problem?
>
> Many thanks
>
> Development environment:  gnat 3.12p under Windows NT 4.0
>
> --
> Philippe Tarroux                        LIMSI-CNRS
> email: tarroux@limsi.fr                 BP 133
> Phone: +(33) 1 69 85 81 23              91470 Orsay cedex - France
> Fax  : +(33) 1 69 85 80 20
>
> --------------F0270765BB3E3E0CA9C37A7A
> Content-Type: text/x-vcard; charset=us-ascii;
>  name="tarroux.vcf"
> Content-Transfer-Encoding: 7bit
> Content-Description: Carte pour Philippe Tarroux
> Content-Disposition: attachment;
>  filename="tarroux.vcf"
>
> begin:vcard
> n:Tarroux;Philippe
> tel;fax:+(33) 1 69 85 80 88
> tel;work:+(33) 1 69 85 81 23
> x-mozilla-html:TRUE
> org:LIMSI-CNRS
> adr:;;BP 133;Orsay;;91403;France
> version:2.1
> email;internet:tarroux@limsi.fr
> fn:Philippe Tarroux
> end:vcard
>
> --------------F0270765BB3E3E0CA9C37A7A--
>
>


Sent via Deja.com
http://www.deja.com/



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

end of thread, other threads:[~2000-12-22  7:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-18 12:36 Embedded hierarchy Philippe Tarroux
2000-12-22  7:11 ` mark_lundquist

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