comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Is it possible to make a possibly self-referential type using containers?
Date: Sat, 30 Jul 2016 21:41:24 -0500
Date: 2016-07-30T21:41:24-05:00	[thread overview]
Message-ID: <nnjogd$14l$1@franka.jacob-sparre.dk> (raw)
In-Reply-To: 18dfc647-ef22-4052-b6ad-ce3516124085@googlegroups.com

You could, of course, create a container using a formal incomplete type 
(which does match incomplete types). The issue(*) is that you'd need to pass 
in a constructor function and a destructor procedure (since you can't 
allocate an object of an incomplete type). That would look something like:

    generic
         type Index_Type is range <>;
         type Element_Type;
         with function "=" (Left, Right : Element_Type)  return Boolean is 
<>;
         with function Copy_Constructor (Obj : Element_Type) return access 
Element_Type;
         with procedure Destructor (Value : access Element_Type);
    package Incomplete_Vectors is
      ....

Then the operations that add elements to the container would use 
Copy_Constructor to allocate a copy, and the clean-up code would use 
Destructor. (It might be better to use a named access for this purpose, the 
problem with that is that you'd have to declare it visible before the 
instance, while that type is really an implementation artifact best kept 
private to this usage.)

                 Randy Brukardt.

(*) It's *almost* possible to create a container of pre-existing objects 
using a formal incomplete type. The problem is that you're not allowed to 
take 'Access of a parameter of such a type (that violates 3.10.1(10/3) - 
even though there's no real problem with that particular operation). I've 
suggested "fixing" this issue in the Ada Standard, but that hasn't gotten 
any traction as most see it as a curiosity rather than the realistic 
technique. If you'd like to see this work, send something to Ada-Comment!


"Shark8" <onewingedshark@gmail.com> wrote in message 
news:18dfc647-ef22-4052-b6ad-ce3516124085@googlegroups.com...
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:

Type Executable_Code(<>) is private; -- The type in which we store 
executable code.
Type Word(<>);

Package Word_List is new Ada.Containers.Indefinite_Vectors
  (Element_Type => Word, Index_Type => Positive);

Type Word( Is_List ) is record
  case Is_List is
    when True  => List : Word_List.Vector;
    when False => Item : Executable_Code;
end record;

The above would be nicely elegant insofar as maintenance goes as it's simple 
and straightforward. However, we cannot use that because Element_Type cannot 
be a incomplete-type, but it needs to be because the full type declaration 
requires the instantiation of the container. -- It also seems that 
Limited/private with clauses cannot help.

* Word: a chunk of code to execute or a list of words [to execute].
** Something like SEQUENCE OF INTEGER is fundamental in IDL and ASN.1. 


  reply	other threads:[~2016-07-31  2:41 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 [this message]
2016-07-31  5:36 ` Jeffrey R. Carter
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