comp.lang.ada
 help / color / mirror / Atom feed
* class wide iterable (and indexable)
@ 2019-01-02 15:48 George Shapovalov
  2019-01-02 17:39 ` Simon Wright
  2019-01-26 22:11 ` George Shapovalov
  0 siblings, 2 replies; 76+ messages in thread
From: George Shapovalov @ 2019-01-02 15:48 UTC (permalink / raw)


Hello,

I apologize if something like this is a common thing, but search here or on google does not turn up much of directly related info, and I cannot quite understand why the following confuses gnat..

The intention here is to create a top-level interface providing iterable/indexable, well, interface, with specific derived types providing appropriate data handling (say to let user select either dynamic, Ada.Containers.Vectors based, or fixed, based on plain arrays storage). 

See here for working code:
https://github.com/gerr135/ada_gems/tree/master/list_iface

So, I would have 
package Lists is ..
type List_Interface is interface
        with Constant_Indexing => List_Constant_Reference,
            Variable_Indexing => List_Reference,
            Default_Iterator  => Iterate,
            Iterator_Element  => Element_Type;

package Lists.Dynamic is ..
type List is new List_Interface with private ..

package Lists.Fixed is ..
type List is new List_Interface with private..

with all appropriate elements declared as abstract and derived types implementing them - either directly attaching Vector interface or wrapping around base array..

Then common code could be kept in class-wide methods, using the new loop forms, like in:
--
for item of List loop
  do_something(item);
end loop;
as well as direct indexing, i.e. List(i)
--
with specific storage types selected as needed in client procedures. In most optimistic scenario only declarative region would need light changes..

This all works well in fact, as long as I operate on specific types. But as soon as I try to use class-wide variable, the "of" form of the loop seems to confuse the compiler.
Thus, e.g. 

LL : Lists.Dynamic.List;
for l of LL loop .. -- works fine

but
LC : Lists.List_Interface'Class := Lists.Dynamic.To_Vector(5);
for l of LC loop .. -- confuses the compiler

And I just cannot see what's so special about this class-wide here. It all seems to follow exactly the same form (by design it passes all compiler checks up to this point).

Please see here for the code:
https://github.com/gerr135/ada_gems/tree/master/list_iface

the specific error is:
test_list_iface.adb:91:17: expected type "Standard.Integer"
test_list_iface.adb:91:17: found private type "Cursor" defined at lists.ads:32, instance at line 22
(and one more line further down that is clearly derivative)

This is not quite a 5-line example code, but implementing iterable/indexable interfaces with proper inheritance is somewhat beasty (even if its utility is in concise end-point code). I am afraid this is the minimal code that would illustrate the point (cut and reformed from larger project where I was trying such approach. Perhaps I should turn to iterating over some index type, which needs to be provided anyway. Seems to be simpler and as readable, but what is there the new interface for, if not to be tried out? :) ).

I would be thankful for any pointers, as I am at a loss what that class-wide for loop is getting confused about. Or why it even expects the Index type in the "of" loop?

Thank you for any pointers.

George

^ permalink raw reply	[flat|nested] 76+ messages in thread
* Re: class wide iterable (and indexable)
@ 2019-01-29  7:45 Randy Brukardt
  2019-01-29 19:34 ` Niklas Holsti
  0 siblings, 1 reply; 76+ messages in thread
From: Randy Brukardt @ 2019-01-29  7:45 UTC (permalink / raw)


"Shark8" <onewingedshark@gmail.com> wrote in message
news:f4c1e192-9a24-40d1-abd1-233ef6087658@googlegroups.com...
> On Wednesday, January 23, 2019 at 3:47:43 PM UTC-7, Randy Brukardt wrote:
>> "Shark8" wrote in message
>> news:9eedf818-8bfb-465b-afe5-aa3fb0525948...
>> > Wouldn't this be a non-issue if we could Static_Predicate Array-types
>> > like
>> > so?
>> >
>> > Type Some_String is Array(Positive range <>) of Character
>> >  with Static-Predicate => Some_String'First = 1;
>>
>> The idea of a static predicate is for them to stand-in for proper set
>> constraints (I would have preferred the latter, but I was alone on that),
>> so
>> they don't have anything to do with array bounds.
>
> What do you mean by "proper set constraints"? [And how would they look?]
> Things like {x| x in {2**y where y in 0..15} }? (X in
> 1|2|4|8|16|..|32_768)

See AI05-0153-2. The idea was that there was a kind of constraint that
represented a set. I used "when" in the syntax to reflect a case statement
limb:

    subtype Odd is Natural when 1|3|5|7|9;

The main difference semantically from static predicates was that these
actually changed the value set, which eliminated (at least in my mind) the
oddities with 'First and 'Last that occur for predicates. Otherwise, their
pretty much the same (including the banning of arrays and slices to
eliminate discontiguous arrays).

I was pretty much a group of one with this idea, so the AI was never really
vetted. So don't trust it too much.

                       Randy.

[Sorry about breaking the thread, I got a bunch of header errors when 
attempting to send a reply.]



^ permalink raw reply	[flat|nested] 76+ messages in thread

end of thread, other threads:[~2019-01-29 20:26 UTC | newest]

Thread overview: 76+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-02 15:48 class wide iterable (and indexable) George Shapovalov
2019-01-02 17:39 ` Simon Wright
2019-01-02 18:11   ` George Shapovalov
2019-01-03  8:52     ` Simon Wright
2019-01-03  9:30       ` George Shapovalov
2019-01-03 16:45         ` Jeffrey R. Carter
2019-01-04  4:32       ` Shark8
2019-01-05  9:03         ` Randy Brukardt
2019-01-03 22:56     ` Randy Brukardt
2019-01-04  0:00       ` George Shapovalov
2019-01-04  8:43         ` Dmitry A. Kazakov
2019-01-04 12:20           ` George Shapovalov
2019-01-05 23:29             ` Jere
2019-01-05 23:50               ` Jere
2019-01-06  9:34                 ` George Shapovalov
2019-01-06 10:19                   ` Dmitry A. Kazakov
2019-01-06 11:30                     ` George Shapovalov
2019-01-06 12:45                       ` Dmitry A. Kazakov
2019-01-06 13:18                         ` George Shapovalov
2019-01-06 14:13                           ` Dmitry A. Kazakov
2019-01-06 16:33                             ` George Shapovalov
2019-01-06 18:29                               ` George Shapovalov
2019-01-06 20:32                                 ` Dmitry A. Kazakov
2019-01-06 21:47                                   ` George Shapovalov
2019-01-07  9:37                                     ` Niklas Holsti
2019-01-07 16:24                                       ` George Shapovalov
2019-01-06 20:18                               ` Dmitry A. Kazakov
2019-01-06 21:58                                 ` George Shapovalov
2019-01-07  8:28                                   ` Dmitry A. Kazakov
2019-01-05  9:21           ` Randy Brukardt
2019-01-05 10:07             ` Dmitry A. Kazakov
2019-01-05 18:17               ` George Shapovalov
2019-01-05 20:07                 ` Simon Wright
2019-01-05 20:41                   ` George Shapovalov
2019-01-07 21:07               ` Randy Brukardt
2019-01-08  9:51                 ` Dmitry A. Kazakov
2019-01-08 19:25                   ` Björn Lundin
2019-01-08 23:26                   ` Randy Brukardt
2019-01-09 17:06                     ` Dmitry A. Kazakov
2019-01-09 23:38                       ` Randy Brukardt
2019-01-10  8:53                         ` Dmitry A. Kazakov
2019-01-10 22:14                           ` Randy Brukardt
2019-01-11  9:09                             ` Dmitry A. Kazakov
2019-01-14 22:59                               ` Randy Brukardt
2019-01-15  9:34                                 ` Dmitry A. Kazakov
2019-01-18 15:48                                   ` Olivier Henley
2019-01-18 16:08                                     ` Dmitry A. Kazakov
2019-01-18 16:29                                       ` Olivier Henley
2019-01-18 16:54                                         ` Dmitry A. Kazakov
2019-01-18 17:31                                           ` Olivier Henley
2019-01-18 18:51                                             ` Shark8
2019-01-18 20:09                                             ` Dmitry A. Kazakov
2019-01-21 23:15                                     ` Randy Brukardt
2019-01-22  8:56                                       ` Dmitry A. Kazakov
2019-01-22 22:00                                         ` Randy Brukardt
2019-01-23  8:14                                           ` Dmitry A. Kazakov
2019-01-22 17:04                                       ` Jeffrey R. Carter
2019-01-22 22:02                                         ` Randy Brukardt
2019-01-23 18:00                                           ` Jeffrey R. Carter
2019-01-23 20:14                                           ` Shark8
2019-01-23 22:47                                             ` Randy Brukardt
2019-01-24 17:11                                               ` Dmitry A. Kazakov
2019-01-28 15:54                                               ` Shark8
2019-01-28 17:23                                                 ` Dmitry A. Kazakov
2019-01-08 18:32                 ` G. B.
2019-01-05 17:05             ` Jeffrey R. Carter
2019-01-05 20:18               ` Dmitry A. Kazakov
2019-01-05 21:09               ` Shark8
2019-01-06 10:11                 ` Jeffrey R. Carter
2019-01-05 20:46             ` Shark8
2019-01-06  9:43               ` Dmitry A. Kazakov
2019-01-26 22:11 ` George Shapovalov
2019-01-26 22:14   ` George Shapovalov
  -- strict thread matches above, loose matches on Subject: below --
2019-01-29  7:45 Randy Brukardt
2019-01-29 19:34 ` Niklas Holsti
2019-01-29 20:26   ` Dmitry A. Kazakov

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