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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.107.18.200 with SMTP id 69mr10323523ios.87.1517005977476; Fri, 26 Jan 2018 14:32:57 -0800 (PST) X-Received: by 10.157.114.140 with SMTP id t12mr1102783otj.4.1517005977278; Fri, 26 Jan 2018 14:32:57 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.am4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!w142no1088029ita.0!news-out.google.com!b73ni1777ita.0!nntp.google.com!g80no1087608itg.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Fri, 26 Jan 2018 14:32:56 -0800 (PST) In-Reply-To: <3af9cfc4-d434-4d60-a4ed-17a16f75717f@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2a01:e35:2ecd:88c0:39d4:7156:83b1:cb42; posting-account=O1Kt4QoAAABKYAjrg-cGai_vZLnN2LEw NNTP-Posting-Host: 2a01:e35:2ecd:88c0:39d4:7156:83b1:cb42 References: <61ba3677-0041-4dba-af9b-a5df48f3ce8a@googlegroups.com> <6427a793-91a4-4feb-b067-ed89b4c04421@googlegroups.com> <3af9cfc4-d434-4d60-a4ed-17a16f75717f@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <36f3a79b-0db5-4199-9a7a-cbf64058cd18@googlegroups.com> Subject: Re: Iterable container as generic parameter From: Lionel Draghi Injection-Date: Fri, 26 Jan 2018 22:32:57 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 5930 X-Received-Body-CRC: 159252767 Xref: reader02.eternal-september.org comp.lang.ada:50167 Date: 2018-01-26T14:32:56-08:00 List-Id: Thank you both for all this, it gave me food for thought. I moved forward using signature package on the format aspect of my list. I want my generic to be able to print the same list this way : [A, B, C]=20 or this way : - A - B - C So I exported the Format aspects to a List_Format signature generic. generic Prefix : String :=3D ""; Postfix : String :=3D ""; Separator : String :=3D ""; -- etc. package List_Format is end;=20 And I created some predefined variant :=20 package Bracketed_List_Format is new List_Format (Prefix =3D> "[", Postfix =3D> "]", Separator =3D> ", "); It's now the last formal parameter of List_Image : (current implementation, I have note yet taken into account Randy last sugg= estion) generic type Element_Type (<>) is Private; with function Image (Element : Element_Type) return String is <>; type Cursor is private; with function First return Cursor is <>; with function Next (Position : Cursor) return Cursor is <>; with function Length return Ada.Containers.Count_Type is <>; with function Element (Position : Cursor) return Element_Type is <>; with package Format is new List_Format (<>); function List_Image return String; The current List_Image generic has 7 formal parameters. Adding the printing options is about 8 more formal parameters, that a lot f= or a typical use. =20 The code is functionally close to what I want as a first version, I will no= w try to ease the use (the instantiation) by "defaulting" as much as possib= le formal parameters. By the way, I have another question. There is no way to provide a default i= nstantiation to a formal package parameter? I would like to do something like : with package Format is new List_Format :=3D Bracketed_List_Format; ------------------------- =20 Lionel=20 Le 26/01/2018 =C3=A0 10:09, ...emmanuel... a =C3=A9crit : >> But I probably would just use your original spec and add the container t= ype >> and iterator function, something like: >> >> generic >> type Cursor is (<>); >> with function Image (C : Cursor) return String; >> type Container (<>) is private; >> with package Iterator_Interfaces is >> new Ada.Iterator_Interfaces (Cursor, others =3D> <>); >> with function Iterator (C : Container) return >> Iterator_Interfaces.Forward_Iterator'Class; >> function List_Image return String; >> >=20 > I think that's a good starting point. > When this works, and if you have a similar need for another algorithm, th= en you can start thinking of signature packages and introduce some of them. >=20 > I started my work on the traits containers when I wanted to write algorit= hms that would work for any of the standard containers (later on I realized= that those containers, at least in the GNAT implementation) had a lot of d= uplicate code between definite/indefinite variants, that could be factored = away with an Elements traits package, so I introduced that. And so on. >=20 > I definitely agree with Randy here that we should not construct signature= packages in the abstract, but start from existing and working code. >=20 > The only drawback is that introducing them breaks the existing API: > - instantiating the generic now uses different set of formals. > We have proposed http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s= /ai12-0205-1.txt?rev=3D1.2&raw=3DN for the next revision of the language, w= hich might help a little >=20 > - moving out some code out of an existing package and into a generic = means you need to introduce a lot of renamings to preserve existing API. > We have proposed http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/= ai12-0229-1.txt?rev=3D1.3&raw=3DN to help. >=20 > I recently made changes to GNATCOLL so that people could chose which kind= of reference counting they want to use for GNATCOLL.Strings. Either no cou= nter (in which case we don't do copy-on-write), non-atomic counters or atom= ic counters. This could only be done via a signature package, since declari= ng a type with aspect "Atomic" generates some memory fence operations which= are slow. That leads to a 3 to 4% speed improvement. Just another example = of possible signature package. >=20 > Emmanuel >=20 --=20 -- Lionel