From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,887bac6875d2db34 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!p69g2000hsa.googlegroups.com!not-for-mail From: Gene Newsgroups: comp.lang.ada Subject: Re: Access idiom Date: Mon, 21 Jan 2008 21:10:26 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <6j5lj.309998$Fc.80333@attbi_s21> <26cde0d5-2c5e-4316-aad7-95fd7269646a@c23g2000hsa.googlegroups.com> NNTP-Posting-Host: 70.101.174.178 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1200978627 1546 127.0.0.1 (22 Jan 2008 05:10:27 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 22 Jan 2008 05:10:27 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p69g2000hsa.googlegroups.com; posting-host=70.101.174.178; posting-account=-BkjswoAAACC3NU8b6V8c50JQ2JBOs04 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648),gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:19525 Date: 2008-01-21T21:10:26-08:00 List-Id: On Jan 21, 10:56=A0pm, Gene wrote: > On Jan 21, 1:15=A0pm, "Jeffrey R. Carter" > > wrote: > > Gene wrote: > > > > I'm at an impasse developing a graph data structure. =A0There are many= > > > node types derived from a "most general" one. =A0Most of the node type= s > > > contain fields that are classwide access to child nodes. =A0Various > > > primitive procedures operate on nodes, dispatching on unnamed node > > > access types. =A0In many cases, the operations return such an access > > > child value. =A0Other "identity" ops just return the dispatching > > > parameter as classwide access. > > > There should be no need for public access types or values in such a data= > > structure. Hiding them should make the package easier to use, easier to > > implement, and safer. > > First, I think the collective wisdom has put me straight. Thanks. > > Another, more succinct explanation... =A0The graph has many node types, > all with common ancestor. =A0Edges are pointers--named or anonymous > access Node_Type'Class; don't care which. =A0Required is a primitive > "transform" procedure that dispatches on node type and must return a > pointer to an _arbitrary node type_ (i.e. again of type access > Node_Type'Class). =A0Frequently the transform should just return its > parameter without modification. =A0Using primitive functions (which > would enable return of anonymous access Node_Type'Class) is not good > due to need for multiple return values. > > Jeff, in view of all the above, a named "access all Node_Type'Class" > type seems necessary for the return values. > > I failed earlier because the out parameter wouldn't convert > automatically from named to unnamed access, and I didn't realize type > conversion would work on L-values. > > Further comments very welcome if there is a better way. =A0Again, > thanks. > > This compiles and dispatches as expected: > > ---- hoo.ads > package Hoo is > > =A0 =A0type Node_Type is abstract tagged null record; > =A0 =A0type Node_Ptr_Type is access all Node_Type'Class; > =A0 =A0procedure XForm(P : access Node_Type; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Rtn : out Node_Ptr_Type) is abstrac= t; > > =A0 =A0type Binary_Type is new Node_Type with record > =A0 =A0 =A0 Left, Right : access Node_Type'Class; > =A0 =A0end record; > > =A0 =A0overriding > =A0 =A0procedure XForm(P : access Binary_Type; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Rtn : out Node_Ptr_Type); > end Hoo; > > ---- hoo.adb > package body Hoo is > > =A0 =A0overriding > =A0 =A0procedure XForm > =A0 =A0 =A0(P : access Binary_Type; > =A0 =A0 =A0 Rtn : out Node_Ptr_Type) > =A0 =A0is > =A0 =A0begin > =A0 =A0 =A0 Rtn :=3D Node_Ptr_Type(P); > =A0 =A0end XForm; > > end Hoo; > > ---- foo.adb > procedure Foo is > =A0 =A0P : access Binary_Type; > begin > =A0 =A0P :=3D new Binary_Type; > =A0 =A0Xform(P.Left, Node_Ptr_Type(P.Left)); > end Foo; I guess this classifies as a "duh..." Declaring graph edges as Node_Ptr_Type instead of access Node_Type'Class eliminates need for type conversion at call site... So now the 2nd last line above is just Xform(P.Left, P.Left); Entlich ist es ganz sauber! Fascinating that changing all the declaration _and_ implementation code from Xform(P : in Node_type; Rtn : out Node_Ptr_type); to Xform(P : access Node_Type; Rtn : out Node_Ptr_type); was possible with only find-and-replace edits (including deletions of many .all's) ... good evidence of the orthogonality and consistency of Ada syntax. Thanks for remarks that access params incur a performance hit. In this app, clean, simple syntax is far more important than performance. All the best, Gene