comp.lang.ada
 help / color / mirror / Atom feed
From: agate!howland.reston.ans.net!noc.near.net!inmet!spock!stt@ucbvax.Berkeley .EDU  (Tucker Taft)
Subject: Re: Access Discrimnants
Date: 30 Jun 93 15:23:13 GMT	[thread overview]
Message-ID: <C9FxEq.FLr@inmet.camb.inmet.com> (raw)

In article <20mdas$ps9@huon.itd.adelaide.edu.au> 
  andrewd@achilles.cs.adelaide.edu.au (,2285592, Andrew Dunstan) writes:

>>
>>    type Set is limited private;
>>      -- Abstract "Set" of Item_Type type
>>    procedure Add_Item(To : in out Set; Item : Item_Type);
>>    . . .
>>
>>    type Set_Iterator(Over : access Set) is limited private;
>>      -- Iterator over a given set, designated by the access discrim
>
>Given these declarations, can one say something like:
>
>function some_func(s1,s2 : set) return set is
>  result : set;
>  it1 : iterator(s1'access) -- or it2 : iterator(s2'unchecked_access);
>
>Of course, s1 and s2 could be copied to local aliased sets, but that
>would seem to be a bit of a waste if one were sure no dangling
>references would be created.

You cannot take 'Access of a formal parameter, unless it is of a tagged
type.  Hence, you will have to make it visible that Set is a tagged type.
[Aside: The reason we restrict 'Access on formals to tagged types
 is that most other types can be passed by copy; for tagged types
 we require pass-by-reference.]
As you mentioned in your followup, you will probably want
to make Set a nonlimited private type, so you can do assignment,
etc.  So this now becomes:

    type Set is tagged private;

Internally Set will probably be derived from Finalization.Controlled,
so you can define your own Duplicate and Finalize operations
(for assignment and scope exit).

Another issue is that access discriminants give read-write access to their
designated objects.  Hence, you can't initialize one with a pointer
to a constant (and IN parameters are considered constants).
So you will have to make the parameters IN OUT mode, which means
you have to change over to a procedure (since functions can't have
IN OUT parameters).

An alternative solution to both of the above problems is to
use access parameters for the function to begin with:

     function Some_Func(S1, S2 : access Set) return Set is
       Result : Set;
       It1 : Iterator(S1);  
    . . .

then you put the 'Access on the call to Some_Func:

     Print_Set(Some_Func(Set1'Access, Set2'Access));

>Sigh. I'll get the hang of this soon. I just can't wait for GNAT to
>come out so that I can start experimenting instead of just reading 
>and writing and asking questions!

Keep those questions coming.  Probably many other people have
similar questions anyway.

>cheers
>
>andrew
>
>#  Andrew Dunstan                   #   There's nothing good or bad   #
>#  net:                             #                                 #
>#    adunstan@steptoe.adl.csa.oz.au #   but thinking makes it so.     #
>#  or: andrewd@cs.adelaide.edu.au   #                                 #

S. Tucker Taft    stt@inmet.com
Ada 9X Mapping/Revision Team
Intermetrics, Inc.
Cambridge, MA  02138

             reply	other threads:[~1993-06-30 15:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-06-30 15:23 Tucker Taft [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-07-01 13:28 Access Discrimnants munnari.oz.au!metro!basser.cs.su.oz.au!news.adelaide.edu.au!achilles!andr
1993-06-28 10:12 munnari.oz.au!yoyo.aarnet.edu.au!news.adelaide.edu.au!achilles!andrewd
1993-06-28  9:23 munnari.oz.au!yoyo.aarnet.edu.au!news.adelaide.edu.au!achilles!andrewd
replies disabled

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