comp.lang.ada
 help / color / mirror / Atom feed
* statically compatible access discriminants in derivation
@ 1998-01-13  0:00 Pat Rogers
  1998-01-16  0:00 ` Nick Roberts
  1998-01-17  0:00 ` Tucker Taft
  0 siblings, 2 replies; 3+ messages in thread
From: Pat Rogers @ 1998-01-13  0:00 UTC (permalink / raw)



Given a tagged type with an access discriminant designating a
classwide type, and a child type derived from this parent type, is it
possible to declare an access discriminant in the child type that is
statically compatible with the parent's access discriminant, while having
the child's access discriminant designate a classwide type further down in
the parent's designated derivation class?  (It's easier to see in the code
below.)

I want to constrain an access discriminant to designate only certain kinds
of objects, i.e., those further down in the derivation class, so that I
can take advantage of the additional information provided by the more
specific view.  I could use the inherited access discriminant without
specifying one for the child type, and convert the inherited discriminant
to the intended classwide type, but that seems rather weak in comparison.

For example, consider the following declarations (ignore the names; I've
chosen them for example purposes only):

package device_if is -- device interface
  type object is abstract tagged null record;
end device_if;

with device_if;
package unit_if is -- unit interface
  type object( board : access device_if.object'class ) is abstract tagged
limited null record;
end unit_if;

Given a concrete instance of device_if.object, I want to use that
classwide type in the access discriminant for a concrete instance of
unit_if.object:

with device_if;
package device_instance is
  type object is new device_if.object with
    record
      x : integer;
    end record;
end device_instance;

with device_instance;
with unit_if;
package unit_instance is
  type object( board : access device_instance.object'class ) is
    new unit_if.object( board ) with null record;
  procedure foo( this : in out object );
end unit_instance;

That way I can directly reference X within foo:

package body unit_instance is
  procedure foo( this : in out object ) is
  begin
    this.board.x := 0;
  end foo;
end unit_instance;

As I read the RM, it seems that 3.7(15) requires them to statically match,
and 4.9.1 says that they can not.  Is there a way around this?

As I mentioned, I could just inherit the discriminant 'board' designating
device_if.object'class, and convert to device_instance.object'class, but
would rather have it checked when the objects are declared.

Any ideas?







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

* Re: statically compatible access discriminants in derivation
  1998-01-13  0:00 statically compatible access discriminants in derivation Pat Rogers
@ 1998-01-16  0:00 ` Nick Roberts
  1998-01-17  0:00 ` Tucker Taft
  1 sibling, 0 replies; 3+ messages in thread
From: Nick Roberts @ 1998-01-16  0:00 UTC (permalink / raw)



I think if you look closely at the wording of 4.9.1(4), you will see that
in the derivation definition

   type object( board : access device_instance.object'class ) is new
unit_if.object( board ) with null record;

the subtype of the board in the parent type (unit_if.object) is 'statically
compatible' with the subtype of the new discriminant, because (a) it is an
access type, and (b) the parent type's discriminant's subtype is
unconstrained.  All class-wide types are unconstrained.

So I believe you can do what you put in the example.

I cannot claim to be authoritative here: others may know better than me
(please, don't laugh, I have my pride you know :-).

-- 

Nick Roberts
Croydon, UK

Proprietor, ThoughtWing Software; Independent Software Development
Consultant
* Nick.Roberts@dial.pipex.com * Voicemail & Fax +44 181-405 1124 *
*** Eats three shredded spams every morning for breakfast ***


Pat Rogers <progers@acm.org> wrote in article
<69gucb$l9k$1@uuneo.neosoft.com>...
> Given a tagged type with an access discriminant designating a
> classwide type, and a child type derived from this parent type, is it
> possible to declare an access discriminant in the child type that is
> statically compatible with the parent's access discriminant, while having
> the child's access discriminant designate a classwide type further down
in
> the parent's designated derivation class? 
> [etc...]





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

* Re: statically compatible access discriminants in derivation
  1998-01-13  0:00 statically compatible access discriminants in derivation Pat Rogers
  1998-01-16  0:00 ` Nick Roberts
@ 1998-01-17  0:00 ` Tucker Taft
  1 sibling, 0 replies; 3+ messages in thread
From: Tucker Taft @ 1998-01-17  0:00 UTC (permalink / raw)



Pat Rogers (progers@acm.org) wrote:

: ...
: with device_instance;
: with unit_if;
: package unit_instance is
:   type object( board : access device_instance.object'class ) is
:     new unit_if.object( board ) with null record;
:   procedure foo( this : in out object );
: end unit_instance;

: That way I can directly reference X within foo:

: package body unit_instance is
:   procedure foo( this : in out object ) is
:   begin
:     this.board.x := 0;
:   end foo;
: end unit_instance;

: As I read the RM, it seems that 3.7(15) requires them to statically match,
                                   "compatible", not "match" ---------^^^^^
: and 4.9.1 says that they can not.  Is there a way around this?

Reread 3.7(15).  It uses the term "statically compatible" not "statically
match"  Because anonymous access subtypes are always unconstrained,
there is no problem of compatibility, presuming the expression used
in the constraint is convertible to the anonymous access type (which 
it is).  

: As I mentioned, I could just inherit the discriminant 'board' designating
: device_if.object'class, and convert to device_instance.object'class, but
: would rather have it checked when the objects are declared.

: Any ideas?

The code above should compile.  Is it being rejected by your
favorite Ada compiler?  You might post the error messages you
are receiving.  Perhaps the real problem is elsewhere.

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




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

end of thread, other threads:[~1998-01-17  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-13  0:00 statically compatible access discriminants in derivation Pat Rogers
1998-01-16  0:00 ` Nick Roberts
1998-01-17  0:00 ` Tucker Taft

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