comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Problem with generic linked list package
Date: Sun, 10 Aug 2014 22:57:11 +0100
Date: 2014-08-10T22:57:11+01:00	[thread overview]
Message-ID: <lyr40oknyg.fsf@pushface.org> (raw)
In-Reply-To: 105213a0-7faa-48b1-a4ba-07791cd97c60@googlegroups.com

Laurent <daemon2@internet.lu> 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.


  reply	other threads:[~2014-08-10 21:57 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-28 13:26 Problem with generic linked list package Laurent
2014-07-29  7:51 ` Jacob Sparre Andersen
2014-07-29  8:12   ` Laurent
2014-07-29  8:27 ` Stephen Leake
2014-07-29 15:38   ` Laurent
2014-08-07 19:07     ` Laurent
2014-08-07 19:21       ` Adam Beneschan
2014-08-07 19:25         ` Adam Beneschan
2014-08-07 22:20           ` Laurent
2014-08-07 23:35             ` Adam Beneschan
2014-08-08  4:42               ` Laurent
2014-08-09 14:32                 ` Laurent
2014-08-09 14:58                   ` AdaMagica
2014-08-09 15:22                   ` Jeffrey Carter
2014-08-09 18:51                   ` Shark8
2014-08-09 21:53                     ` Laurent
2014-08-09 22:25                       ` Jeffrey Carter
2014-08-10  8:22                       ` Simon Wright
2014-08-10 10:45                         ` Simon Wright
2014-08-10 20:20                           ` Laurent
2014-08-10 21:57                             ` Simon Wright [this message]
2014-08-10 23:42                               ` Jeffrey Carter
2014-08-11  4:51                                 ` Laurent
2014-08-11  5:13                                   ` Jeffrey Carter
2014-08-11  7:56                                     ` Laurent
2014-08-07 20:37       ` Shark8
2014-08-07 22:30         ` Laurent
2014-08-07 23:22           ` Shark8
replies disabled

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