comp.lang.ada
 help / color / mirror / Atom feed
* Generalized Iterators
@ 2015-12-29 11:50 AdaMagica
  2015-12-29 22:12 ` Randy Brukardt
  2016-01-05  1:54 ` Lucretia
  0 siblings, 2 replies; 4+ messages in thread
From: AdaMagica @ 2015-12-29 11:50 UTC (permalink / raw)


I'm experiencing problems with generalized iterators. The code below is reduced
to the minimum in order to be compilable and show the problem. Note that the
body of Circularly_Linked_Lists is not given since not needed to produce the
problem.

The code follows strictly the examples given in the Ada 2012 Rationale.
Expecially the function Iterate is defined thus:

  function Iterate (CLL: not null access Circularly_Linked_List)
    return Forward_Iterator'Class;

This produces the compiler message (GNAT GPL 2015)

  for Cursor in List.Iterate loop
ausprobieren.adb:15:21: expect object that implements iterator interface

Note that the aspects Default_Iterator and Iterator_Element are not needed as
long as the loop above uses "in" rather than "of".

If the function Iterate is replaced by (note the place of the attribute Class)

  function Iterate (CLL: not null access Circularly_Linked_List'Class)
    return Forward_Iterator;

the loop works as expected. When I then add the aspects Default_Iterator
and Iterator_Element and try to use the "of" loop, I get

  for Int of List loop
ausprobieren.adb:19:14: expected an access type with designated type "Circularly_Linked_List'Class" defined at circularly_linked_lists.ads:9, instance at line 8
ausprobieren.adb:19:14: found private type "Circularly_Linked_List" defined at circularly_linked_lists.ads:9, instance at line 8
ausprobieren.adb:19:14:   ==> in call to "Iterate" at circularly_linked_lists.ads:32, instance at line 8
ausprobieren.adb:19:14:   ==> in call to "Iterate" at circularly_linked_lists.ads:32, instance at line 8
ausprobieren.adb:19:19: invalid prefix in selected component "I280b"
    Put_Line (Integer'Image (Int));
  end loop;

I can understand this since Iterate has the list as an access parameter,
and the compiler tries to construct the "in" loop from the default iterator.
But also this does not work:

  for Int of List'Access loop

Is this a problem of GNAT or is there anything I just don't see?

Also in Ada.Containers.Doubly_Linked_Lists, Iterate is defined like so with
the Class attribute at the iterator:

   function Iterate (Container : List)
      return List_Iterator_Interfaces.Reversible_Iterator'Class;

Code below:
------------------------
with Ada.Iterator_Interfaces;

generic

  type Element_Type is private;

package Circularly_Linked_Lists is

  type Circularly_Linked_List is tagged private
    with -- Default_Iterator  => Iterate,
         -- Iterator_Element  => Element_Type,
         Variable_Indexing => Acc;

  type Accessor (Elem: not null access Element_Type) is limited null record
    with Implicit_Dereference => Elem;

  type Cursor is private;

  function Has_Element (Position: Cursor) return Boolean;

  function Acc (CLL     : in out Circularly_Linked_List;
                Position: in     Cursor) return Accessor;

  package Iterator_Interfaces is new Ada.Iterator_Interfaces (Cursor, Has_Element);

  type Forward_Iterator (CLL: not null access Circularly_Linked_List) is new Iterator_Interfaces.Forward_Iterator with null record;

  overriding function First (Object  : Forward_Iterator) return Cursor;
  overriding function Next  (Object  : Forward_Iterator;
                             Position: Cursor          ) return Cursor;

--function Iterate (CLL: not null access Circularly_Linked_List'Class) return Forward_Iterator;
  function Iterate (CLL: not null access Circularly_Linked_List      ) return Forward_Iterator'Class;

private

  type CLL_Ptr is access all Circularly_Linked_List;

  type Cursor is record
    Current: CLL_Ptr;
  end record;

  type Circularly_Linked_List is tagged record
    Next, Prev: CLL_Ptr;
  end record;

end Circularly_Linked_Lists;
with Ada.Text_IO;
use  Ada.Text_IO;

with Circularly_Linked_Lists;

procedure Ausprobieren is

  package Lists is new Circularly_Linked_Lists (Integer);
  use Lists;

  List: aliased Circularly_Linked_List;

begin

  for Cursor in List.Iterate loop
    Put_Line (Integer'Image (List (Cursor)));
  end loop;

end Ausprobieren;


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

* Re: Generalized Iterators
  2015-12-29 11:50 Generalized Iterators AdaMagica
@ 2015-12-29 22:12 ` Randy Brukardt
  2015-12-31 10:33   ` AdaMagica
  2016-01-05  1:54 ` Lucretia
  1 sibling, 1 reply; 4+ messages in thread
From: Randy Brukardt @ 2015-12-29 22:12 UTC (permalink / raw)


"AdaMagica" <christ-usch.grein@t-online.de> wrote in message 
news:aea9159f-c02c-4a08-915a-504227424bd9@googlegroups.com...
> I'm experiencing problems with generalized iterators. The code below is 
> reduced
> to the minimum in order to be compilable and show the problem. Note that 
> the
> body of Circularly_Linked_Lists is not given since not needed to produce 
> the
> problem.
>
> The code follows strictly the examples given in the Ada 2012 Rationale.
> Expecially the function Iterate is defined thus:
>
>  function Iterate (CLL: not null access Circularly_Linked_List)
>    return Forward_Iterator'Class;
>
> This produces the compiler message (GNAT GPL 2015)
>
>  for Cursor in List.Iterate loop
> ausprobieren.adb:15:21: expect object that implements iterator interface
>
...
I get the same result with the compiler I have. (It's probably newer than 
GPL 2015, but I'm not certain as it is a wavefront for a problem last year.)

...
> I can understand this since Iterate has the list as an access parameter,
> and the compiler tries to construct the "in" loop from the default 
> iterator.
> But also this does not work:
>
>  for Int of List'Access loop
>
> Is this a problem of GNAT or is there anything I just don't see?

Looks like a problem of GNAT. Didn't we discuss something like this example 
within the last few months here??

                                    Randy.


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

* Re: Generalized Iterators
  2015-12-29 22:12 ` Randy Brukardt
@ 2015-12-31 10:33   ` AdaMagica
  0 siblings, 0 replies; 4+ messages in thread
From: AdaMagica @ 2015-12-31 10:33 UTC (permalink / raw)


Am Dienstag, 29. Dezember 2015 23:13:01 UTC+1 schrieb Randy Brukardt:
> Looks like a problem of GNAT. Didn't we discuss something like this example 
> within the last few months here??

Don't know, can't remember...
> 
>                                     Randy.

Thank you very much, Randy. And Happy New Year - and a prosperous one at that.

I've been pondering this problem for days, and I hesitated to write a bug report since my last one was a complete flop (I was blind like a bat).

Now I'll send it in.

Christoph

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

* Re: Generalized Iterators
  2015-12-29 11:50 Generalized Iterators AdaMagica
  2015-12-29 22:12 ` Randy Brukardt
@ 2016-01-05  1:54 ` Lucretia
  1 sibling, 0 replies; 4+ messages in thread
From: Lucretia @ 2016-01-05  1:54 UTC (permalink / raw)


Just tried it on gcc-4.9.2, I get:

GNATMAKE 4.9.2
Copyright (C) 1992-2014, Free Software Foundation, Inc.
  "ausprobieren.ali" being checked ...
  -> "ausprobieren.ali" missing.
gcc -c ausprobieren.adb
ausprobieren.adb:15:31: invalid prefix in call
End of compilation
gnatmake: "ausprobieren.adb" compilation error

Using the first Iterate function:

GNATMAKE 4.9.2
Copyright (C) 1992-2014, Free Software Foundation, Inc.
  "ausprobieren.ali" being checked ...
  -> "ausprobieren.ali" missing.
gcc -c ausprobieren.adb
ausprobieren.adb:15:31: invalid prefix in call
End of compilation
gnatmake: "ausprobieren.adb" compilation error

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

end of thread, other threads:[~2016-01-05  1:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-29 11:50 Generalized Iterators AdaMagica
2015-12-29 22:12 ` Randy Brukardt
2015-12-31 10:33   ` AdaMagica
2016-01-05  1:54 ` Lucretia

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