comp.lang.ada
 help / color / mirror / Atom feed
* access on function returning a class wide type
@ 2002-11-19 21:38 alex
  2002-11-20  0:11 ` James S. Rogers
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: alex @ 2002-11-19 21:38 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 819 bytes --]


Is there a �workaround/trick� to implement this kind of think.

package A is
   type A_T is abstract tagged null record;
   function Fa return A_T is abstract;
   type Fa_Ptr is access function return A_T'Class;
end A;

with A;
package B is
  type B_T is new A.A_T with null record ;
  function Fa return B_T;
end B;

package body B is
  function Fa return B_T is
  begin return (A.A_T with null record);
  end Fa;
end B;

with A,B;
procedure Test is
   Ptr : A.Fa_Ptr := B.Fa'access; -- error :  LRM:8.6(28)
begin null;
end Test;

The compiler is expecting a function returning a A_T�Class, ok I understand�
But, B_T is in the A_T�Class!


-- 
Ce message a ete poste via la plateforme Web club-Internet.fr
This message has been posted by the Web platform club-Internet.fr

http://forums.club-internet.fr/



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

* Re: access on function returning a class wide type
  2002-11-19 21:38 access on function returning a class wide type alex
@ 2002-11-20  0:11 ` James S. Rogers
  2002-11-20  6:39   ` alex
  2002-11-20  8:43 ` Jean-Pierre Rosen
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: James S. Rogers @ 2002-11-20  0:11 UTC (permalink / raw)



"alex" <xela@netcourrier.com> wrote in message
news:20021119-223832-295143@foorum.com...
>
> Is there a "workaround/trick" to implement this kind of think.
>
> package A is
>    type A_T is abstract tagged null record;
>    function Fa return A_T is abstract;
>    type Fa_Ptr is access function return A_T'Class;
> end A;
>
> with A;
> package B is
>   type B_T is new A.A_T with null record ;
>   function Fa return B_T;
> end B;
>
> package body B is
>   function Fa return B_T is
>   begin return (A.A_T with null record);
>   end Fa;
> end B;
>
> with A,B;
> procedure Test is
>    Ptr : A.Fa_Ptr := B.Fa'access; -- error :  LRM:8.6(28)
> begin null;
> end Test;
>
> The compiler is expecting a function returning a A_T'Class, ok I
understand.
> But, B_T is in the A_T'Class!

B_T is in the A_T'Class but a function returning B_T does not return a
class-wide type. You have defined Fa_Ptr to be an access type to a
function returning a class-wide type rooted in type A_T.

Another problem you have is that you are trying to return an instance
of an abstract type. You cannot create an instance of an abstract type.

Jim Rogers





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

* Re: access on function returning a class wide type
  2002-11-20  0:11 ` James S. Rogers
@ 2002-11-20  6:39   ` alex
  0 siblings, 0 replies; 6+ messages in thread
From: alex @ 2002-11-20  6:39 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 896 bytes --]


I agree with you and I understood the compiler error. The explanation is exactly 
what your are saying : B_T is in the A_T'Class but a function returning B_T does 
not return a
class-wide type. You have defined Fa_Ptr to be an access type to a
function returning a class-wide type rooted in type A_T. Also in my example I do 
not really need to work with abstract type. In fact what I am intending to do is 
not to compile this exact program, but to be able to create a Class (here A_T) 
and a pointer on functions which would be �usable/available� for all functions 
defined in the hierarchy (i.e. Ptr : A.Fa_Ptr := B.Fa'access). Also, in my 
�design issue� I can�t use Class wide type except in package A).
Thanks.

-- 
Ce message a ete poste via la plateforme Web club-Internet.fr
This message has been posted by the Web platform club-Internet.fr

http://forums.club-internet.fr/



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

* Re: access on function returning a class wide type
  2002-11-19 21:38 access on function returning a class wide type alex
  2002-11-20  0:11 ` James S. Rogers
@ 2002-11-20  8:43 ` Jean-Pierre Rosen
  2002-11-20 12:39 ` Dmitry A. Kazakov
  2002-11-20 15:32 ` Matthew Heaney
  3 siblings, 0 replies; 6+ messages in thread
From: Jean-Pierre Rosen @ 2002-11-20  8:43 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 617 bytes --]


"alex" <xela@netcourrier.com> a �crit dans le message news:
20021119-223832-295143@foorum.com...

> The compiler is expecting a function returning a A_T'Class, ok I
understand.
> But, B_T is in the A_T'Class!
>
Repeat after me:
For any tagged type T, T'Class is a type different from T.

Not just some magic formula saying "well you can put anybody from the family
in here". It is a type in the Ada sense.
Therefore, in your example, the profiles don't match.

--
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





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

* Re: access on function returning a class wide type
  2002-11-19 21:38 access on function returning a class wide type alex
  2002-11-20  0:11 ` James S. Rogers
  2002-11-20  8:43 ` Jean-Pierre Rosen
@ 2002-11-20 12:39 ` Dmitry A. Kazakov
  2002-11-20 15:32 ` Matthew Heaney
  3 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2002-11-20 12:39 UTC (permalink / raw)


On 19 Nov 2002 21:38:32 GMT, alex <xela@netcourrier.com> wrote:

>Is there a �workaround/trick� to implement this kind of think.
>
>package A is
>   type A_T is abstract tagged null record;
>   function Fa return A_T is abstract;
>   type Fa_Ptr is access function return A_T'Class;
>end A;
>
>with A;
>package B is
>  type B_T is new A.A_T with null record ;
>  function Fa return B_T;
>end B;
>
>package body B is
>  function Fa return B_T is
>  begin return (A.A_T with null record);
>  end Fa;
>end B;
>
>with A,B;
>procedure Test is
>   Ptr : A.Fa_Ptr := B.Fa'access; -- error :  LRM:8.6(28)
>begin null;
>end Test;
>
>The compiler is expecting a function returning a A_T�Class, ok I understand�
>But, B_T is in the A_T�Class!

You could wrap B.Fa in a function returning A_T'Class:

function Fa_b return A_T'Class is
begin
   return B.Fa;
end Fa_b;

The question is why do you need something like that? Sort of lazy
object creation?

---
Regards,
Dmitry Kazakov
www.dmitry-kazakov.de



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

* Re: access on function returning a class wide type
  2002-11-19 21:38 access on function returning a class wide type alex
                   ` (2 preceding siblings ...)
  2002-11-20 12:39 ` Dmitry A. Kazakov
@ 2002-11-20 15:32 ` Matthew Heaney
  3 siblings, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 2002-11-20 15:32 UTC (permalink / raw)


alex <xela@netcourrier.com> wrote in message news:<20021119-223832-295143@foorum.com>...
> 
> with A,B;
> procedure Test is
>    Ptr : A.Fa_Ptr := B.Fa'access; -- error :  LRM:8.6(28)
> begin null;
> end Test;
> 
> The compiler is expecting a function returning a A_T'Class, ok I understand�
> But, B_T is in the A_T'Class!

Do this instead:

procedure Test is
   B_Object : aliased B_T'Class := Fa;
   Ptr : constant Fa_Ptr := B_Object'Unchecked_Access;
begin
   null;
end;



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

end of thread, other threads:[~2002-11-20 15:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-19 21:38 access on function returning a class wide type alex
2002-11-20  0:11 ` James S. Rogers
2002-11-20  6:39   ` alex
2002-11-20  8:43 ` Jean-Pierre Rosen
2002-11-20 12:39 ` Dmitry A. Kazakov
2002-11-20 15:32 ` Matthew Heaney

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