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=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Is it possible to make a possibly self-referential type using containers? Date: Sat, 30 Jul 2016 22:36:22 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <18dfc647-ef22-4052-b6ad-ce3516124085@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 31 Jul 2016 05:36:30 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="d7c030f56102b58a2c16dea977db88bb"; logging-data="12658"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SJ3auAwBbJEhmUXc1QRYn+vp6fJ0OLlI=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <18dfc647-ef22-4052-b6ad-ce3516124085@googlegroups.com> Cancel-Lock: sha1:Ol+I5tmItFxJx69SrTSM+IZW3mc= Xref: news.eternal-september.org comp.lang.ada:31228 Date: 2016-07-30T22:36:22-07:00 List-Id: 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