0

What is the error in this Ada2012 program?

with Ada.Iterator_Interfaces; package My is type Cursor is private; function Has_Element (Position: Cursor) return Boolean; package Base_Iterators is new Ada.Iterator_Interfaces(Cursor, Has_Element); type Bindings_Iterator is new Base_Iterators.Forward_Iterator with private; overriding function First (Object: Bindings_Iterator) return Cursor; overriding function Next (Object: Bindings_Iterator; Position: Cursor) return Cursor; private type Iterated_Object is access all Integer; type Cursor is new Iterated_Object; type Bindings_Iterator is new Base_Iterators.Forward_Iterator with null record; end My; 

Attempt to check the syntax and semantics:

$ gnatgcc -gnat2012 -c my.ads my.ads:23:09: type must be declared abstract or "First" overridden my.ads:23:09: "First" has been inherited from subprogram at a-iteint.ads:26, instance at line 9 

As far as I understand First is overridden by me. I don't get what the compiler complaints for.

6
  • Out of curiosity; how do you plan on releasing the memory allocated by Cursor objects? Commented Aug 21, 2017 at 19:24
  • @egilhh This is a shortened version of the real code. In the real code there were no Integer pointers. There was a pointer to an object. Commented Aug 21, 2017 at 19:26
  • it's still an access type, and you'll need to free the memory... Commented Aug 21, 2017 at 19:28
  • @egilhh note it is access all, and all here is because I access to an object which may be hold in a local variable not in a memory pool. So there is not need to deallocate it, as it is goes away when the local variable goes out of scope (this is the most likely use case of my code) Commented Aug 21, 2017 at 19:35
  • this design will not work with generalized for loops. There is nowhere in the API there for a user to add an access to a local variable. Besides, burdening the user with an extra stack variable per cursor object seems cumbersome Commented Aug 21, 2017 at 19:46

1 Answer 1

3

The error comes from Cursor being a privately derived access type. Changing it to

type Cursor is access all Integer; 

removes the error, as does changing it to a record type or numeric type. Moving the full definition of Iterated_Object and Cursor to the public view also removes the error.

I'm thinking you have stumbled upon a compiler error.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the advice how to change my code. It is a useful advice. But you have not explained the exact version why my code does not work. Please help with this too. I don't understand
I will report the bug

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.