comp.lang.ada
 help / color / mirror / Atom feed
* Problem implementing Iterators
@ 2015-01-27 13:54 Laurent
  2015-01-27 16:24 ` AdaMagica
  0 siblings, 1 reply; 10+ messages in thread
From: Laurent @ 2015-01-27 13:54 UTC (permalink / raw)


Hi

I tried to add an iterator to my double linked list using the example from John Barnes Ada 2005 book, chapter about iterators.

For the moment the only procedure using the iterator is the Display one.

In my test app I get this error when I compile it:

   package GL_String is
     new Double_Linked_Lists_Generic (Element_Type    => Name_Type,
                                      Display_Element => Display_String );
L1 : GL_String.List ;

   GL_String.Display (L1); <-- error

expected an access type with designated private type "List" defined at double_linked_lists_generic.ads:7, instance at line 23 
found private type "List" defined at double_linked_lists_generic.ads:7, instance at line 23 

List and Iterator are defined as: 

 type List is limited private;
 type Iterator (L : access List) is limited private;

(...)
private

   type List_Node;
   type List_Ptr is access List_Node;
   type List_Node is record
      Element : Element_Type;
      Next    : List_Ptr;
      Previous:List_Ptr;
   end record;

   type List is record <-- example in the book is a list without head/tail
      Head : List_Ptr;
      Tail : List_Ptr;
   end record;

   type Iterator (L : access List) is limited
      record
         This :  List_Ptr;
      end record;

So how do I get this to work?

Thanks


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

* Re: Problem implementing Iterators
  2015-01-27 13:54 Problem implementing Iterators Laurent
@ 2015-01-27 16:24 ` AdaMagica
  2015-01-27 17:26   ` Laurent
  0 siblings, 1 reply; 10+ messages in thread
From: AdaMagica @ 2015-01-27 16:24 UTC (permalink / raw)


>    package GL_String is
>      new Double_Linked_Lists_Generic (Element_Type    => Name_Type,
>                                       Display_Element => Display_String );
> L1 : GL_String.List ;
> 
>    GL_String.Display (L1); <-- error

Try
     GL_String.Display (L1'Access);

That's what the error message tells you:

> expected an access type with designated private type "List" defined at double_linked_lists_generic.ads:7, instance at line 23 
> found private type "List" defined at double_linked_lists_generic.ads:7, instance at line 23 

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

* Re: Problem implementing Iterators
  2015-01-27 16:24 ` AdaMagica
@ 2015-01-27 17:26   ` Laurent
  2015-01-27 17:55     ` Björn Lundin
  2015-01-28 16:42     ` AdaMagica
  0 siblings, 2 replies; 10+ messages in thread
From: Laurent @ 2015-01-27 17:26 UTC (permalink / raw)



> Try
>      GL_String.Display (L1'Access);

Tried it, now getting:
prefix of "Access" attribute must be aliased

Remember having read something about "aliased" but don't remember what it is good for or how it is used. Probably haven't understood it either.

>That's what the error message tells you:

This access stuff is still very alien too me, that's also the reason why I still play around with my linked list package. Even if it is of no use.

https://github.com/Chutulu/Chapter-15.git

Thanks


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

* Re: Problem implementing Iterators
  2015-01-27 17:26   ` Laurent
@ 2015-01-27 17:55     ` Björn Lundin
  2015-01-27 18:06       ` Laurent
  2015-01-28 16:42     ` AdaMagica
  1 sibling, 1 reply; 10+ messages in thread
From: Björn Lundin @ 2015-01-27 17:55 UTC (permalink / raw)


On 2015-01-27 18:26, Laurent wrote:
> 
>> Try
>>      GL_String.Display (L1'Access);
> 
> Tried it, now getting:
> prefix of "Access" attribute must be aliased
> 

change
L1 : GL_String.List ;
to
L1 : aliased GL_String.List ;

'aliased' makes it allowed to use 'access on a variable.
Others can then see that it can be manipulated
via pointers.




if it still does not work,
try (if on gnat)

L1'unchecked_access
instead of L1'access



--
Björn


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

* Re: Problem implementing Iterators
  2015-01-27 17:55     ` Björn Lundin
@ 2015-01-27 18:06       ` Laurent
  2015-01-27 18:43         ` Björn Lundin
  0 siblings, 1 reply; 10+ messages in thread
From: Laurent @ 2015-01-27 18:06 UTC (permalink / raw)



> change
> L1 : GL_String.List ;
> to
> L1 : aliased GL_String.List ;

Ok thanks works now. An easier solution than what I tried. Was writing the message while you posted yours :)

>Think (/= knowing) I have found the beginning of the solution
>
>   type Element_Type is private;
>   type Element_Ptr is access all Element_Type;
>   with procedure Display_Element (Item : in Element_Type);
>
>(...)
>
>private
>
>   type List_Node;
>   type List_Ptr is access List_Node;
>   type List_Node is record
>      Element : aliased Element_Ptr;
>      Next    : List_Ptr;
>      Previous:List_Ptr;
>   end record;
>
>the only problem I have now is that when I want to instantiate the package I get:
>
>23:4 missing actual "Element_Ptr"?
>
>When I changed Element_Type to Element_Ptr I got the message that Element_Type is missing? >Or do I have to provide both?

Would that actually have worked? Perhaps the additional Element_Ptr was't needed at all?

Thanks


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

* Re: Problem implementing Iterators
  2015-01-27 18:06       ` Laurent
@ 2015-01-27 18:43         ` Björn Lundin
  2015-01-27 20:58           ` Jeffrey Carter
  0 siblings, 1 reply; 10+ messages in thread
From: Björn Lundin @ 2015-01-27 18:43 UTC (permalink / raw)


On 2015-01-27 19:06, Laurent wrote:
> 
>> change
>> L1 : GL_String.List ;
>> to
>> L1 : aliased GL_String.List ;
> 
> Ok thanks works now. An easier solution than what I tried. Was writing the message while you posted yours :)

...

> Would that actually have worked? Perhaps the additional Element_Ptr was't needed at all?
> 

I do not know, but I do not think so.
You need to declare a variable as aliased if you want to use/take its
'access.

As I understand it, its only purpose is to tell the programmer that
this variable may be changed via pointers - beware ...


--
Björn

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

* Re: Problem implementing Iterators
  2015-01-27 18:43         ` Björn Lundin
@ 2015-01-27 20:58           ` Jeffrey Carter
  2015-01-28 12:20             ` Björn Lundin
  0 siblings, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2015-01-27 20:58 UTC (permalink / raw)


On 01/27/2015 11:43 AM, Björn Lundin wrote:
> 
> As I understand it, its only purpose is to tell the programmer that
> this variable may be changed via pointers - beware ...

My understanding is that there are certain optimizations that can be taken with
unaliased objects that cannot be done with aliased objects, so this provides
important information to the compiler.

Some time ago (late 1980s or early 1990s) there was an article in /Ada Letters/
about why the Tartan Ada compilers were able to produce faster code for Ada
versions of their C benchmarks than their C compilers. The fact that all C
variables are aliased, while all Ada-83 variables are not, was one of the factors.

-- 
Jeff Carter
"When Roman engineers built a bridge, they had to stand under it
while the first legion marched across. If programmers today
worked under similar ground rules, they might well find
themselves getting much more interested in Ada!"
Robert Dewar
62

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

* Re: Problem implementing Iterators
  2015-01-27 20:58           ` Jeffrey Carter
@ 2015-01-28 12:20             ` Björn Lundin
  0 siblings, 0 replies; 10+ messages in thread
From: Björn Lundin @ 2015-01-28 12:20 UTC (permalink / raw)


On 2015-01-27 21:58, Jeffrey Carter wrote:
> On 01/27/2015 11:43 AM, Björn Lundin wrote:

> Some time ago (late 1980s or early 1990s) there was an article in /Ada Letters/
> about why the Tartan Ada compilers were able to produce faster code for Ada
> versions of their C benchmarks than their C compilers. The fact that all C
> variables are aliased, while all Ada-83 variables are not, was one of the factors.


Very interesting, thanks


--
Björn

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

* Re: Problem implementing Iterators
  2015-01-27 17:26   ` Laurent
  2015-01-27 17:55     ` Björn Lundin
@ 2015-01-28 16:42     ` AdaMagica
  2015-01-28 20:14       ` Laurent
  1 sibling, 1 reply; 10+ messages in thread
From: AdaMagica @ 2015-01-28 16:42 UTC (permalink / raw)


Am Dienstag, 27. Januar 2015 18:26:38 UTC+1 schrieb Laurent:
> > Try
> >      GL_String.Display (L1'Access);
> 
> Tried it, now getting:
> prefix of "Access" attribute must be aliased

That was to be expected.

> Remember having read something about "aliased" but don't remember what
> it is good for or how it is used. Probably haven't understood it either.

I thought (hoped) you would try to find out yourself.

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

* Re: Problem implementing Iterators
  2015-01-28 16:42     ` AdaMagica
@ 2015-01-28 20:14       ` Laurent
  0 siblings, 0 replies; 10+ messages in thread
From: Laurent @ 2015-01-28 20:14 UTC (permalink / raw)



> I thought (hoped) you would try to find out yourself.

Yes me too, is much more satisfying than being spoonfed.

Wanted to write that in my Ada95 book this aliased/access stuff wasn't explained much but everything is there. Somehow zapped that info. 

But thanks for your efforts/patience

Laurent


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

end of thread, other threads:[~2015-01-28 20:14 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-27 13:54 Problem implementing Iterators Laurent
2015-01-27 16:24 ` AdaMagica
2015-01-27 17:26   ` Laurent
2015-01-27 17:55     ` Björn Lundin
2015-01-27 18:06       ` Laurent
2015-01-27 18:43         ` Björn Lundin
2015-01-27 20:58           ` Jeffrey Carter
2015-01-28 12:20             ` Björn Lundin
2015-01-28 16:42     ` AdaMagica
2015-01-28 20:14       ` Laurent

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