comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Problem with generic linked list package
Date: Thu, 7 Aug 2014 16:35:52 -0700 (PDT)
Date: 2014-08-07T16:35:52-07:00	[thread overview]
Message-ID: <ee3343a8-90db-4f02-9591-5128fca7bc76@googlegroups.com> (raw)
In-Reply-To: <e312c04f-778c-44cf-9032-6f390a5f4b20@googlegroups.com>

On Thursday, August 7, 2014 3:20:00 PM UTC-7, Laurent wrote:
> I thought that if the package was instantiated with an integer for element_type, "<" defined for element_type would "see" that oh yes I know how to compare 2 integers and use the "<" defined in the standart package for integers. Same for char, float whatever.

This is how generics are in Ada.  The compiler needs to know when it's compiling the generic, *before* the generic is instantiated, whether it can compare the types.  Since you said

    type Element_Type is private;

this means Element_Type can be any type (almost), and the compiler therefore won't know whether the type's values can be compared.  If you had said

    type Element_Type is range <>;

then Element_Type could only be instantiated with an integer type, and therefore the compiler would know that the values *can* be compared.  But this isn't the case with a "private" generic type.  Since the compiler can't confirm, when the body is compiled, whether there will be a "<" for the type, the compiler won't let you use "<".  That's what Shark8's solution does: it guarantees that there will be "<" and "=" functions for your type.  When you instantiate the generic, you have the choice of supplying some functions to be used for "<" or "=" (which can be a different name), or if you don't, it will look to see if "<" and "=" are defined.  And if they're not (if you pass it a record type that you haven't defined "<" for), the instantiation will be illegal.


> Didn't think that "<" element_type could be chasing his own tail.

When you write

   function "<" (Left, Right : Element_Type) return Boolean is
   begin
      if Left < Right then ...

Even if there were a "<" defined outside, this would still be infinite recursion, because of Ada's rules about visibility.  It's the same as if you wrote 

   procedure Some_Procedure (X : Integer) is
   begin
        Some_Procedure (X);
        ...

Even if there were some other Some_Procedure somewhere else that could be called, inside the body of Some_Procedure, the name Some_Procedure means itself.  Same for "<".  Inside the body of "<", the < symbol means that function itself (when it's called on the same types of arguments).  There are ways around this, though, for example:

   procedure Some_Procedure (X : Integer) is
   begin
        Some_Other_Package.Some_Procedure (X);
        ...

> 
> 
> Perhaps I am just to naive to believe that the compiler knows  what I want to do.

Compilers are not allowed to read minds.  That would be very bad, because it's important to have well-defined rules about what your code means.  If I put something into a Google search, sometimes Google makes some assumptions about what it thinks I *really* want, but it's OK because if it's wrong, I can just try again.  But it would be bad if compilers were allowed to do that--you could distribute a program but have no idea what it really does, since there wouldn't be any clear rules about how the compiler interprets your code.

                               -- Adam

  reply	other threads:[~2014-08-07 23:35 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 [this message]
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
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