comp.lang.ada
 help / color / mirror / Atom feed
From: Gene <gene.ressler@gmail.com>
Subject: Access idiom
Date: Sun, 20 Jan 2008 11:57:09 -0800 (PST)
Date: 2008-01-20T11:57:09-08:00	[thread overview]
Message-ID: <c41f12c9-9d7d-4ddf-8cb2-458c4195a4b0@m34g2000hsb.googlegroups.com> (raw)

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



             reply	other threads:[~2008-01-20 19:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-20 19:57 Gene [this message]
2008-01-21  1:01 ` Access idiom Ludovic Brenta
2008-01-21  4:16   ` Gene
2008-01-21 15:37     ` Robert A Duff
2008-01-22  4:11     ` Randy Brukardt
2008-01-22  4:11     ` Randy Brukardt
2008-01-21  9:05 ` Dmitry A. Kazakov
2008-01-21 18:15 ` Jeffrey R. Carter
2008-01-22  3:56   ` Gene
2008-01-22  5:10     ` Gene
2008-01-22  9:01     ` Dmitry A. Kazakov
2008-01-22 18:47     ` Jeffrey R. Carter
2008-01-22  4:15   ` Randy Brukardt
2008-01-22  4:15   ` Randy Brukardt
  -- strict thread matches above, loose matches on Subject: below --
2008-01-21  7:12 Grein, Christoph (Fa. ESG)
replies disabled

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