comp.lang.ada
 help / color / mirror / Atom feed
From: briot.emmanuel@gmail.com
Subject: Re: Iterable container as generic parameter
Date: Thu, 25 Jan 2018 10:26:25 -0800 (PST)
Date: 2018-01-25T10:26:25-08:00	[thread overview]
Message-ID: <e6a29e2c-962d-46fb-81ed-5f0fda872ee0@googlegroups.com> (raw)
In-Reply-To: <6427a793-91a4-4feb-b067-ed89b4c04421@googlegroups.com>

Lionel,
This won't provide an answer to your specific question (I also haven't been able to achieve that). Some ideas that you might find interesting.
I have spent some time last year working on "traits containers" (https://github.com/AdaCore/ada-traits-containers for some code). The idea is to split the large set of formal parameters you showed in your example into several smaller sets, which are then passed via a signature package (aka traits).

For instance, to implement your List_Item, we need to know a few things on the container (but not its specific type), and how to iterate. To create the container, we need to know what elements it stores. This could lead to the following (untested) signature packages:

      generic
            type T (<>) is limited private;
            type Stored_T is private;
            with function Store (E : T) return Stored_T;
            with function Retrieve (E : Stored_T) return T;
      package Elements is
      end Elements;
      --  How to store elements in a data structure. If T is definite, we simply copy, but if
      --  if is indefinite, we need to allocate memory for instance.

      generic
            with package Elems is new Elements (<>);
            type Iterable (<>) is limited private;
            type Cursor is private;
            with function First (Self : Iterable) return Cursor;
            with function Next (C : Cursor) return Cursor;
            with function Get (C : Cursor) return Elems.T;
      package Iterables is
      end Iterables;

      generic
           with package Iterable is new Iterables (<>);
            --  A container is an iterable data structure, that we can also modify

           with procedure Append (Self : in out Iterable.Iterable; E : Iterable.Elems.T);
      package Containers is
            subtype Container is Iterable.Iterable;
      end Containers;

Then if you create a specific container type (or wrap one of the predefined Ada containers), you provide an instance of those predefined packages when applicable. As shown above, although the signature packages themselves should contain any code, they can provide instances for other signature packages (think: in C++, a random-access cursor is also a forward cursor).

Finally, your algorithm is generic in terms of those generic packages:

      generic
           with package Iterable is new Iterables (<>);
      function Image (Self : Iterable.Iterable) return String;

It receives an object, for which it knows how to create a cursor and manipulate that cursor. The instances of the signature packages we created above are bricks, and your algorithm works in terms of those bricks.

This is a promising scheme. In fact, the more such elementary bricks you have (one to describe how to store elements in other data structure, one to describe iterables, one to describe hashables, ...) the more interesting it becomes, since you can combine them in unexpected ways.

BUT: my containers were using the GNAT 'Iterable' aspect, not the more complex Ada iterators, and I did not find a way to describe via the generic parameters that a "for..of" loop would work on them. 

This is related to your question, but unfortunately (and sorry) does not provide a solution.

Emmanuel


  reply	other threads:[~2018-01-25 18:26 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-25  0:22 Iterable container as generic parameter Lionel Draghi
2018-01-25  3:36 ` Randy Brukardt
2018-01-25 14:58   ` Lionel Draghi
2018-01-25 18:26     ` briot.emmanuel [this message]
2018-01-26  4:50     ` Randy Brukardt
2018-01-26  9:09       ` briot.emmanuel
2018-01-26 22:32         ` Lionel Draghi
2018-01-27  7:03           ` Randy Brukardt
2018-01-27 11:38             ` Lionel Draghi
2018-01-27 14:31               ` Jere
2018-01-28 18:08         ` Lionel Draghi
2018-01-29 13:34           ` briot.emmanuel
2018-01-29 22:26             ` Lionel Draghi
2018-01-29 23:00               ` Randy Brukardt
2018-01-30 22:35                 ` Lionel Draghi
2018-01-30 23:32                   ` Lionel Draghi
2018-01-31  0:15                   ` Lionel Draghi
2018-01-30 15:13               ` Shark8
2018-01-26 23:07 ` Lionel Draghi
replies disabled

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