comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Is it possible to make a possibly self-referential type using containers?
Date: Sat, 30 Jul 2016 22:36:22 -0700
Date: 2016-07-30T22:36:22-07:00	[thread overview]
Message-ID: <nnk2os$cbi$1@dont-email.me> (raw)
In-Reply-To: <18dfc647-ef22-4052-b6ad-ce3516124085@googlegroups.com>

On 07/30/2016 05:31 PM, Shark8 wrote:
> Consider something like Forth, IDL, and ASN.1 where there recursion plays a big part in the underlying ideas: the former in its definition of "Word"*, and in the latter two in the defining of types**.
> 
> So, given that the most straightforward way to implement these would be in a self-referencing type it would be nice to be able to use containers for this, perhaps like:

There was a discussion of this on here about a year ago in the context of having
a private type that is self-referential using a container. See

https://groups.google.com/d/msg/comp.lang.ada/SO9olMGfF4o/aGrCFMsVCwAJ

The advantage of using a container is that one doesn't have to deal with the
error-prone aspects of using access types and doing memory management.

I presented a solution there that used a tagged type, an indefinite container of
the 'Class of that type, and a type extension to achieve this:

with Ada.Containers.Indefinite_Vectors;

package P is
   type T (<>) is private;
private
   type Root is tagged null record;

   package Lists is new Ada.Containers.Vectors
      (Index_Type => Positive, Element_Type => Root'Class);

   type T is new Root with record
      List : Lists.Vector;
   end T;
end P;

The intention here is that only values of type T will be stored in such a
vector, so Root and the type extension for T are noise. You also have to
explicitly convert anything retrieved from the list to type T, which is more
noise. But it beats messing with access values.

-- 
Jeff Carter
"I was either in love, or I had smallpox."
Take the Money and Run
137


  parent reply	other threads:[~2016-07-31  5:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-31  0:31 Is it possible to make a possibly self-referential type using containers? Shark8
2016-07-31  2:41 ` Randy Brukardt
2016-07-31  5:36 ` Jeffrey R. Carter [this message]
2016-07-31  7:00 ` Dmitry A. Kazakov
replies disabled

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