comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Trouble with overriding and class-wide parameters
Date: Sat, 15 Jul 2006 20:00:03 +0200
Date: 2006-07-15T20:00:02+02:00	[thread overview]
Message-ID: <l9r0i2ruslfk.14mw9cu6esnqd.dlg@40tude.net> (raw)
In-Reply-To: 44b91f16$0$27041$626a54ce@news.free.fr

On Sat, 15 Jul 2006 19:00:00 +0200, Yves Bailly wrote:

> Hello all,
> 
> Here's a problem I'm encountering, while developping a (rather
> huge) library using Ada 2005, for now using only the latest GNAT
> GPL compiler from AdaCore (i.e. GNAT-2006).
> 
> Given a first package, defining a tagged type:
> --8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<---
> package Pkg is
>    type Some_Type is tagged null record;
>    not overriding
>    procedure Setup(st: not null access Some_Type;
>                    p :          access Some_Type'Class);
> end Pkg;
> --8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<---
> The procedure just Put_Line() a string to identify itself.
> 
> Then take a second package, with a type derived from the previous:
> --8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<---
> with Pkg; use Pkg;
> package Sub_Pkg is
>    type Sub_Type is new Some_Type with null record;
>    not overriding
>    procedure Setup(st: not null access Sub_Type;
>                    p :          access Sub_Type'Class);
> end Sub_Pkg;
> --8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<---
> Here again the procedure just print something to identify itself.
> 
> Now the test program:
> --8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<---
> with Pkg; use Pkg;
> with Sub_Pkg; use Sub_Pkg;
> procedure Test is
>    st: aliased Sub_Type;
> begin
>    st.Setup(null);
> end Test;
> --8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<---
> 
> I would expect the call to Setup() to invoke the Sub_Pkg.Setup()
> procedure, as "st" is of type Sub_Type. However the procedure
> actually called is Pkg.Setup(), as shown by the output :
> Pkg.Setup(Some_Type)
> 
> What am I missing? I suspect the value "null" to be part of this,
> but I can't find a workaround.

What are you trying to achieve? The code looks wrong to me. Why are you 
using access types in a procedure? Why do you want to overload two 
different Setup? If the second parameter has to be covariant then Ada does 
support it:

   procedure Setup (St : in out Some_Type; p : in out Some_Type);

If you want it contravariant Ada supports that as well:

   procedure Setup (St : in out Some_Type; p : in out Some_Type'Class);

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2006-07-15 18:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-15 17:00 Trouble with overriding and class-wide parameters Yves Bailly
2006-07-15 18:00 ` Dmitry A. Kazakov [this message]
2006-07-15 18:20   ` Yves Bailly
2006-07-15 19:12     ` Dmitry A. Kazakov
2006-07-15 18:52 ` Jeffrey R. Carter
2006-07-15 20:03   ` Yves Bailly
2006-07-16  0:20     ` Jeffrey R. Carter
2006-07-16 10:00 ` Javier Miranda
2006-07-16 12:11   ` Ludovic Brenta
2006-07-16 13:36     ` Javier Miranda
replies disabled

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