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,start X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!m34g2000hsb.googlegroups.com!not-for-mail From: Gene Newsgroups: comp.lang.ada Subject: Access idiom Date: Sun, 20 Jan 2008 11:57:09 -0800 (PST) Organization: http://groups.google.com Message-ID: NNTP-Posting-Host: 70.101.174.178 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1200859029 30463 127.0.0.1 (20 Jan 2008 19:57:09 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 20 Jan 2008 19:57:09 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: m34g2000hsb.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:19486 Date: 2008-01-20T11:57:09-08:00 List-Id: First, thanks for past help from this group. I'm at an impasse developing a graph data structure. There are many node types derived from a "most general" one. Most of the node types contain fields that are classwide access to child nodes. Various primitive procedures operate on nodes, dispatching on unnamed node access types. In many cases, the operations return such an access child value. Other "identity" ops just return the dispatching parameter as classwide access. Here is the idea: ----- hoo.ads ---- package Hoo is type Node_Type is tagged record I : Integer := 0; end record; function Op(P : access Node_Type) return access Node_Type'Class; type Threat_Type is new Node_Type with record Target : access Node_Type'Class; end record; overriding function Op(P : access Threat_Type) return access Node_Type'Class; end Hoo; ---- hoo.adb ---- package body Hoo is function Op(P : access Node_Type) return access Node_Type'Class is begin return P; -- identity operation end Op; overriding function Op(P : access Threat_Type) return access Node_Type'Class is begin return P.Target; -- just return a child end Op; end Hoo; ---- foo.adb (main procedure) ---- with Hoo; use Hoo; procedure Foo is Threat : access Threat_Type; begin -- Nonsense just to verify type compatibility. Threat := new Threat_Type; Threat.Target := Op(Threat); end Foo; ----- This is all fine. The trouble is that some of the primitive ops need to return multiple values, some of which will be classwide access to node. The "natural" implementation seems to be a procedure with several "out" parameters; but, "seem" is the operative word. To use out parameters, the classwide access node type must be named. The problem here is I can't see a way to dispatch on this named access type, which will be necessary to recursively operate on child nodes. One idea is to return a record type that holds the needed multiple values, but this seems ugly. It will require defining a bunch of ephemeral record types that don't have clear meaning as objects. I tentatively have given up on access dispatching entirely, dispatching instead on node types (not access) and copying to form fresh values of a named classwide access type for the return value(s). This same named type is used for node child pointers. While this okay, there is a lot of useless copying. Feels like I'm playing whack-a-mole with the Ada type system. What's the idomatic way to get this job done without the copying? Thanks, Gene