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!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Problem with generic linked list package Date: Sun, 10 Aug 2014 22:57:11 +0100 Organization: A noiseless patient Spider Message-ID: References: <85a97spo1c.fsf@stephe-leake.org> <92dd7d22-a7da-44d7-9c40-b3aa62881683@googlegroups.com> <4f431041-5292-434d-988e-46d69f4800f8@googlegroups.com> <472a5732-3dc9-4c08-8bda-00720375a2a9@googlegroups.com> <8cc86d41-2c7e-42d7-b057-1f9f010d9002@googlegroups.com> <75864d79-609c-4e37-98d8-6b5f050ad59d@googlegroups.com> <42e0f58b-63c4-4a08-90ce-b5ddf7ebe95b@googlegroups.com> <105213a0-7faa-48b1-a4ba-07791cd97c60@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx05.eternal-september.org; posting-host="b814ca014269fd1fa21bc4c05b94dd9e"; logging-data="31505"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uYy7GtSbXHLcKZeZThv7W51O88tKXVGA=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (darwin) Cancel-Lock: sha1:cAfs92dejvac5Xp25g4HVVJSYYo= sha1:HGCmWIzF7/X5ApcaVmuIyGrfWa0= Xref: news.eternal-september.org comp.lang.ada:21647 Date: 2014-08-10T22:57:11+01:00 List-Id: Laurent writes: (actually I wrote this next section) > type Rec is record > B : Boolean; > I : Integer; > end record; > function "<" (L, R : Rec) return Boolean is > begin > return L.I < R.I; > end "<"; > Would that work for a record like this or would I have to define a > function for every imaginable record? > > ie: type Rec is record > S: String; > I: Integer; > D: Date; > end record; > > Or could I do this (just a little piece of code hope you understand > what I mean): > > els if Element.Rec.I < L.Head.all.Element.Rec.I then > Add_To_Front (L, Element); You have (after Shark8's suggestion, I think) a generic package which starts something like generic type T is private; with function "<" (L, R : T) return Boolean is <>; package P is ... If you want to instantiate it with a type Rec1 type Rec1 is record B : Boolean; I : Integer; end record; then you have somehow to provide a function to take the place of the "<" in the generic formal part. package P_For_Rec1 is new P (T => Rec1, "<" => ?something?); where ?something? is a function which takes two parameters of type Rec1 and returns Boolean. It will be a Good Thing if ?something? returns True if the first parameter is "less than" the second, *whatever "less than" means in your application*. If you want the comparison to be on the I component of Rec1, then you could supply Less where function Less (L, R : Rec1) return Boolean is (L.I < R.I); (that's in Ada 2012), in which case you'd instantiate with package P_For_Rec1 is new P (T => Rec1, "<" => Less); The "is <>" in the formal part of package P means that you could alternatively write function "<" (L, R : Rec1) return Boolean is (L.I < R.I); and package P_For_Rec1 is new P (T => Rec1); If you now decide you want to instantiate P with type Rec2 is record S: String; I: Integer; D: Date; end record; then you have to decide how function Less (L, R : Rec2) return Boolean is (?); needs to work. Do you need to compare the S component? the I component? the D component? or some combination? But however it has to work, it has to be a new function.