From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!newsfeed.xs3.de!io.xs3.de!news.jacob-sparre.dk!franka.jacob-sparre.dk!pnx.dk!.POSTED.rrsoftware.com!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Iterable container as generic parameter Date: Wed, 24 Jan 2018 21:36:22 -0600 Organization: JSA Research & Innovation Message-ID: References: <61ba3677-0041-4dba-af9b-a5df48f3ce8a@googlegroups.com> Injection-Date: Thu, 25 Jan 2018 03:36:23 -0000 (UTC) Injection-Info: franka.jacob-sparre.dk; posting-host="rrsoftware.com:24.196.82.226"; logging-data="28984"; mail-complaints-to="news@jacob-sparre.dk" X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.7246 Xref: reader02.eternal-september.org comp.lang.ada:50137 Date: 2018-01-24T21:36:22-06:00 List-Id: "Lionel Draghi" wrote in message news:61ba3677-0041-4dba-af9b-a5df48f3ce8a@googlegroups.com... >I would like to make a generic function containing this simple code : > > for E of L loop > Put_Line (Image (E)); > end loop; > > And I would like to have this generic compatible with all "iterable > container". > > I thought it would be possible by just providing as generic parameters the > Image function, and an Ada.Iterator_Interfaces instantiation, but without > referencing the container. > Isn't the iterator supposed to encapsulates both the cursor and the > container? > > Something like : > > generic > type Cursor is (<>); > with function Image (C : Cursor) return String; > with package Iterator_Interfaces is > new Ada.Iterator_Interfaces (Cursor, others => <>); > function List_Image return String; > > But I couldn't figure out a simple way to do the body : > 1. using the "for E of L" or the "for C in L.Iterate" loop refers to L, > the Iterable container : why should I need that here? L is the container object that you are iterating over. An iterator interface describes *how* to iterate, but you also have to describe *what* data to iterate. > 2. and it's not clear for me how to directly use a Cursor with First and > Next functions provided by Iterator_Interfaces, that need a > Forward_Iterator parameter. You just call it, prefixing with the container object in question. These are just functions that work like any other functions. --- As to the question you didn't ask, you clearly need the container type somewhere in your interface, so you can pass container objects. (Or, I suppose, you could reference the type used in the instance of the Iterator_Interfaces.) Otherwise, you just have methods but no data. And there isn't anything interesting that you can do with just methods! Randy. > > Any hints? > > Thanks, > > -- > -- Lionel