comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Another problem with "interface"
Date: Mon, 16 Feb 2009 14:29:50 +0100
Date: 2009-02-16T14:29:50+01:00	[thread overview]
Message-ID: <k5psg6d5qlud$.1bdvok5hkszh4.dlg@40tude.net> (raw)
In-Reply-To: mJednUhUY_4tqgTUnZ2dneKdnZydnZ2d@posted.plusnet

On Mon, 16 Feb 2009 09:53:59 +0000, Robert_Matthews wrote:

> In using interface types with GNAT I have encountered another problem.
> Consider the following package:
> 
> package Test is
> 
>    type A_Type is limited interface;
> 
>    procedure P (A : in out A_Type; D : Integer) is abstract;
> 
>    protected type New_A_Type is new A_Type
>    with
>       procedure P (D : Integer);
>       --  other subprograms...
>    private
>       F : Integer;
>    end New_A_Type;
> 
>    function Set_A return New_A_Type;
> 
> end Test;
> 
> GNAT gives an error for the function Set_A: 
> "operation can be dispatching in only one type",
> which leaves me mystified. Am I making another
> dumb mistake? Please help!
> 
> The version of GNAT is GNAT GPL 2008 (20080521).

This looks like a compiler bug to me. The compiler thinks that Set_A is a
protected function of New_A_Type and thus has the hidden argument
New_A_Type and the result New_A_Type, so it complains. But protected type
is not tagged so Set_A cannot be dispatching.

However you can trick the compiler by ensuring that Set_A declaration were
beyond the freezing point of New_A_Type (whatever that might mean for a
protected type). For example:

   type A_Type is limited interface;

   procedure P (A : in out A_Type; D : Integer) is abstract;

   protected type New_A_Type is new A_Type with
      procedure P (D : Integer);
      --  other subprograms...
   private
      F : Integer;
   end New_A_Type;

   package Foo is  -- Package brackets around it
      function Set_A return New_A_Type;
   end Foo;

A more logical way to do it would be:

   function Set_A return New_A_Type'Class;

Alas, this does not work, because there seem to be no way to create
New_A_Type'Class, since New_A_Type is protected.

However you could use A_Type'Class instead:

   function Set_A return A_Type'Class;

P.S. Returning New_A_Type from a function is tricky because it is limited,
yet does not have aggregates. You can use the return statement to work this
around:

   function Set_A return New_A_Type is
   begin
      return Result : New_A_Type do
         null;
      end return;
   end Set_A;

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



  parent reply	other threads:[~2009-02-16 13:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-16  9:53 Another problem with "interface" Robert_Matthews
2009-02-16 10:26 ` christoph.grein
2009-02-16 10:40   ` Georg Bauhaus
2009-02-16 11:27     ` Robert_Matthews
2009-02-16 11:45       ` Egil Høvik
2009-02-16 11:54     ` christoph.grein
2009-02-16 13:29 ` Dmitry A. Kazakov [this message]
2009-02-16 13:56   ` Georg Bauhaus
2009-02-17 16:28 ` Robert_Matthews
replies disabled

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