comp.lang.ada
 help / color / mirror / Atom feed
* A problem with "interface"
@ 2009-02-05 15:05 Robert_Matthews
  2009-02-05 16:13 ` Jacob Sparre Andersen
  2009-02-05 16:14 ` Adam Beneschan
  0 siblings, 2 replies; 4+ messages in thread
From: Robert_Matthews @ 2009-02-05 15:05 UTC (permalink / raw)


In using interface types with GNAT I have encountered a problem.
Consider the following packages:

package Int is

   type A is interface;
   type B is interface;

   procedure P (Param1 : A; Param2 : B'Class) is abstract;

end Int;

with Int;
package Client is

   type Client_A is new Int.A with null record;
   type Client_B is new Int.B with null record;

   overriding
   procedure Client_P (Param1 : Client_A; Param2 : Client_B'Class);

end Client;

On compiling, GNAT says "Client_P" does not override anything.
Am I making a dumb mistake here - or is it GNAT?

The version of GNAT is GNAT GPL 2008 (20080521).

Thanks in advance,

Robert



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: A problem with "interface"
  2009-02-05 15:05 A problem with "interface" Robert_Matthews
@ 2009-02-05 16:13 ` Jacob Sparre Andersen
  2009-02-05 16:14 ` Adam Beneschan
  1 sibling, 0 replies; 4+ messages in thread
From: Jacob Sparre Andersen @ 2009-02-05 16:13 UTC (permalink / raw)


Robert_Matthews wrote:

> In using interface types with GNAT I have encountered a problem.
> Consider the following packages:
> 
> package Int is
> 
>    type A is interface;
>    type B is interface;
> 
>    procedure P (Param1 : A; Param2 : B'Class) is abstract;
> 
> end Int;
> 
> with Int;
> package Client is
> 
>    type Client_A is new Int.A with null record;
>    type Client_B is new Int.B with null record;
> 
>    overriding
>    procedure Client_P (Param1 : Client_A; Param2 : Client_B'Class);
> 
> end Client;

What procedure do you expect "Client_P" to override?

If you happen to want to override the procedure "P", you might want to
make sure the profile matches:

   overriding
   procedure P (Param1 : Client_A; Param2 : Int.B'Class);

Greetings,

Jacob
-- 
"They that can give up essential liberty to obtain a little
 temporary safety deserve neither liberty nor safety."
                                        -- Benjamin Franklin



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: A problem with "interface"
  2009-02-05 15:05 A problem with "interface" Robert_Matthews
  2009-02-05 16:13 ` Jacob Sparre Andersen
@ 2009-02-05 16:14 ` Adam Beneschan
  2009-02-06 14:08   ` Robert_Matthews
  1 sibling, 1 reply; 4+ messages in thread
From: Adam Beneschan @ 2009-02-05 16:14 UTC (permalink / raw)


On Feb 5, 7:05 am, Robert_Matthews <igno...@ramatthews.free-
online.co.uk> wrote:
> In using interface types with GNAT I have encountered a problem.
> Consider the following packages:
>
> package Int is
>
>    type A is interface;
>    type B is interface;
>
>    procedure P (Param1 : A; Param2 : B'Class) is abstract;
>
> end Int;
>
> with Int;
> package Client is
>
>    type Client_A is new Int.A with null record;
>    type Client_B is new Int.B with null record;
>
>    overriding
>    procedure Client_P (Param1 : Client_A; Param2 : Client_B'Class);
>
> end Client;
>
> On compiling, GNAT says "Client_P" does not override anything.
> Am I making a dumb mistake here - or is it GNAT?

You're making a mistake, but I'm hesitant to call it a "dumb" one.
When you declare Client_A, the package implicitly declares inherited
operations, which are inherited from primitive operations of Int.A.
One of those is Client_P, and so you'll get this *implicit*
declaration;

   procedure Client_P (Param1 : Client_A;  Param2 : Int.B'Class);

Since Client_A is derived from Int.A, all uses of "A" in the profile
are replaced by Client_A.  But "B" isn't touched.

Note that nothing is inherited when you declare Client_B.  Client_P is
a primitive operation of Int.A but *not* of Int.B (since its parameter
is B'Class, not B).

When you declare an overriding procedure, the procedure has to
override something that is implicitly declared.  The only implicitly
declared procedure is the one I showed above; its second parameter has
type Int.B'Class, not Client_B'Class, and therefore the procedure you
declared can't override it.

The way to do this is to declare your procedure with "Param2 :
Int.B'Class"; then in the body of Client_P, you can say something like

   if Param2 not in Client_B'Class then
      raise <some_exception> with "Wrong client type";
   end if;

                                 -- Adam






^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: A problem with "interface"
  2009-02-05 16:14 ` Adam Beneschan
@ 2009-02-06 14:08   ` Robert_Matthews
  0 siblings, 0 replies; 4+ messages in thread
From: Robert_Matthews @ 2009-02-06 14:08 UTC (permalink / raw)


Adam Beneschan wrote:

> You're making a mistake, but I'm hesitant to call it a "dumb" one.
> When you declare Client_A, the package implicitly declares inherited
> operations, which are inherited from primitive operations of Int.A.
> One of those is Client_P, and so you'll get this *implicit*
> declaration;
> 
>    procedure Client_P (Param1 : Client_A;  Param2 : Int.B'Class);
> 
> Since Client_A is derived from Int.A, all uses of "A" in the profile
> are replaced by Client_A.  But "B" isn't touched.
> 
> Note that nothing is inherited when you declare Client_B.  Client_P is
> a primitive operation of Int.A but *not* of Int.B (since its parameter
> is B'Class, not B).
> 
> When you declare an overriding procedure, the procedure has to
> override something that is implicitly declared.  The only implicitly
> declared procedure is the one I showed above; its second parameter has
> type Int.B'Class, not Client_B'Class, and therefore the procedure you
> declared can't override it.
> 
> The way to do this is to declare your procedure with "Param2 :
> Int.B'Class"; then in the body of Client_P, you can say something like
> 
>    if Param2 not in Client_B'Class then
>       raise <some_exception> with "Wrong client type";
>    end if;
> 
>                                  -- Adam

Many thanks for putting me right on this - though on a second
look I see I made yet another dumb mistake: in the package "Client"
the procedure should be called "P" rather than "Client_P".

Clearly senility is taking its toll!

Robert




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-02-06 14:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-05 15:05 A problem with "interface" Robert_Matthews
2009-02-05 16:13 ` Jacob Sparre Andersen
2009-02-05 16:14 ` Adam Beneschan
2009-02-06 14:08   ` Robert_Matthews

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