comp.lang.ada
 help / color / mirror / Atom feed
* Another problem with "interface"
@ 2009-02-16  9:53 Robert_Matthews
  2009-02-16 10:26 ` christoph.grein
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Robert_Matthews @ 2009-02-16  9:53 UTC (permalink / raw)


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).

Thanks in advance,

Robert




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

* Re: Another problem with "interface"
  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 13:29 ` Dmitry A. Kazakov
  2009-02-17 16:28 ` Robert_Matthews
  2 siblings, 1 reply; 9+ messages in thread
From: christoph.grein @ 2009-02-16 10:26 UTC (permalink / raw)


I guess you've confused GNAT beyond repair.

The problem is your function Set_A (the rest is OK). Ada 2005 no
longer has return-by-reference functions, so you cannot return an
object of type New_A_Type.



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

* Re: Another problem with "interface"
  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:54     ` christoph.grein
  0 siblings, 2 replies; 9+ messages in thread
From: Georg Bauhaus @ 2009-02-16 10:40 UTC (permalink / raw)


christoph.grein@eurocopter.com schrieb:
> I guess you've confused GNAT beyond repair.
> 
> The problem is your function Set_A (the rest is OK). Ada 2005 no
> longer has return-by-reference functions, so you cannot return an
> object of type New_A_Type.


Uhm, I think that, still, you can call functions
that will assign the (already existing) return
objcect. Which was, IIUC, the point of coining the
word "constructor function".

  X: constant New_A_T := Set_A;

http://www.adacore.com/2007/05/28/gem-3/




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

* Re: Another problem with "interface"
  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
  1 sibling, 1 reply; 9+ messages in thread
From: Robert_Matthews @ 2009-02-16 11:27 UTC (permalink / raw)


Georg Bauhaus wrote:

> Uhm, I think that, still, you can call functions
> that will assign the (already existing) return
> objcect. Which was, IIUC, the point of coining the
> word "constructor function".
> 
>   X: constant New_A_T := Set_A;
> 
> http://www.adacore.com/2007/05/28/gem-3/

Indeed, that is my intent.
Note that if I use an ordinary record type, e.g.

 type New_A_Type is new A_Type with ...

then GNAT compiles things OK; it is when
I use a protected type it complains.

Robert





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

* Re: Another problem with "interface"
  2009-02-16 11:27     ` Robert_Matthews
@ 2009-02-16 11:45       ` Egil Høvik
  0 siblings, 0 replies; 9+ messages in thread
From: Egil Høvik @ 2009-02-16 11:45 UTC (permalink / raw)


On Feb 16, 12:27 pm, Robert_Matthews <igno...@ramatthews.free-
online.co.uk> wrote:
> Georg Bauhaus wrote:
> > Uhm, I think that, still, you can call functions
> > that will assign the (already existing) return
> > objcect. Which was, IIUC, the point of coining the
> > word "constructor function".
>
> >   X: constant New_A_T := Set_A;
>
> >http://www.adacore.com/2007/05/28/gem-3/
>
> Indeed, that is my intent.
> Note that if I use an ordinary record type, e.g.
>
>  type New_A_Type is new A_Type with ...
>
> then GNAT compiles things OK; it is when
> I use a protected type it complains.
>
> Robert

Actually, it is when the protected type inherits an interface it
complains.
For me, at least, (using Gnat Pro 6.1.2) removing the inheritance
compiles OK...



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

* Re: Another problem with "interface"
  2009-02-16 10:40   ` Georg Bauhaus
  2009-02-16 11:27     ` Robert_Matthews
@ 2009-02-16 11:54     ` christoph.grein
  1 sibling, 0 replies; 9+ messages in thread
From: christoph.grein @ 2009-02-16 11:54 UTC (permalink / raw)


> Uhm, I think that, still, you can call functions
> that will assign the (already existing) return
> objcect. Which was, IIUC, the point of coining the
> word "constructor function".

Indeed, construct-in-place is the term, I forgot :-(



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

* Re: Another problem with "interface"
  2009-02-16  9:53 Another problem with "interface" Robert_Matthews
  2009-02-16 10:26 ` christoph.grein
@ 2009-02-16 13:29 ` Dmitry A. Kazakov
  2009-02-16 13:56   ` Georg Bauhaus
  2009-02-17 16:28 ` Robert_Matthews
  2 siblings, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2009-02-16 13:29 UTC (permalink / raw)


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



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

* Re: Another problem with "interface"
  2009-02-16 13:29 ` Dmitry A. Kazakov
@ 2009-02-16 13:56   ` Georg Bauhaus
  0 siblings, 0 replies; 9+ messages in thread
From: Georg Bauhaus @ 2009-02-16 13:56 UTC (permalink / raw)


Dmitry A. Kazakov schrieb:
> 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.

Maybe illustrating this point,


procedure R is
   type I is Synchronized Interface;

   protected type T is new I with
     function Make_T return I'class;
   end T;

   protected body T is
      function Make_T return I'class is
      begin
         return (I with null record);
      end;
   end T;

   X: constant T := T.Make_T;
begin
   null;
end;


+===========================GNAT BUG DETECTED==============================+
| 4.3.0 20070903 (experimental) [trunk revision 128061]
(i686-apple-darwin8) |
| Assert_Failure atree.adb:3812                                            |
| Error detected at r.adb:15:22                                            |
| Please submit a bug report; see http://gcc.gnu.org/bugs.html.





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

* Re: Another problem with "interface"
  2009-02-16  9:53 Another problem with "interface" Robert_Matthews
  2009-02-16 10:26 ` christoph.grein
  2009-02-16 13:29 ` Dmitry A. Kazakov
@ 2009-02-17 16:28 ` Robert_Matthews
  2 siblings, 0 replies; 9+ messages in thread
From: Robert_Matthews @ 2009-02-17 16:28 UTC (permalink / raw)


Thank you for your comments, from which
I conclude that what I am trying to do
is valid Ada2005, but that GNAT cannot
cope - not even, it would seem, the latest
GNAT Pro.
Fortunately I can work around the problem.

Thanks,
Robert





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

end of thread, other threads:[~2009-02-17 16:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2009-02-16 13:56   ` Georg Bauhaus
2009-02-17 16:28 ` Robert_Matthews

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