comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Dynamically tagged expression not allowed. Why?
Date: Sat, 29 May 2010 20:23:36 +0200
Date: 2010-05-29T20:23:37+02:00	[thread overview]
Message-ID: <fn0j6rxg2zwt.1bjdpr0fjjq29.dlg@40tude.net> (raw)
In-Reply-To: c2ead$4c0150a0$4ca4a823$11809@API-DIGITAL.COM

On Sat, 29 May 2010 12:36:33 -0500, Marc A. Criley wrote:

> Stripping out a test case, I see that it has nothing to with the C++ 
> binding per se, but it's an Ada issue that's flummoxing me.  Here's the 
> test code:
> 
> procedure Dyty_Test is
> 
>     type Class_Type is tagged limited record
>        null;
>     end record;
> 
>     function New_Class_Instance return Class_Type'Class;
> 
>     Conn : Class_Type := New_Class_Instance;

Should Conn be mutable? Otherwise you could use a function instead.

>     function New_Class_Instance return Class_Type'Class is
>     begin
>        -- Just a stub
>        return New_Class_Instance;

Infinite recursion. I suppose it is

return X : Class_Type;

>     end New_Class_Instance;
> 
> The error is on the declaration of "Conn", with the error occurring on 
> the invocation of the initializing New_Class_Instance function. I'm just 
> not understanding something here.

You are "returning" a class-wide limited object, which is to "initialize" a
specific object.

A more difficult problem is that to initialize Conn, GNAT wants to know the
body of the initializer.
--------------------------------------------------------------
Variant 1. Two packages (to have the body):

   package P is
      type Class_Type is tagged limited record
         null;
      end record;
      function New_Class_Instance return Class_Type'Class;
      function New_Class_Object return Class_Type;
   end P;

   package P.Instances is
      Conn : Class_Type := New_Class_Object;
   end P.Instances;

   package body P is
      function New_Class_Object return Class_Type is
      begin
         return X : Class_Type;
      end New_Class_Object;
      function New_Class_Instance return Class_Type'Class is
      begin
         return New_Class_Object;
      end New_Class_Instance;
   end P;
----------------------------------------------------------------------------------------------------
Variant 2. Old good Ada initialization (in 80's we knew how to do it right)

   package P is
      type Class_Type is tagged limited record
         null;
      end record;
      function New_Class_Instance return Class_Type'Class;
      Conn : Class_Type; -- It will be OK upon the body elaboration!
   end P;

   package body P is
      procedure Construct (Object : in out Class_Type) is
      begin
         null;
      end Construct;
      function New_Class_Instance return Class_Type'Class is
      begin
         return X : Class_Type do
            Construct (X);
         end return;
      end New_Class_Instance;
   begin
      Construct (Conn); -- Fix it now
   end P;

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



  reply	other threads:[~2010-05-29 18:23 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-29 17:36 Dynamically tagged expression not allowed. Why? Marc A. Criley
2010-05-29 18:23 ` Dmitry A. Kazakov [this message]
2010-06-03  2:37   ` Yannick Duchêne (Hibou57)
2010-06-03  7:48     ` Dmitry A. Kazakov
2010-06-01 23:36 ` Randy Brukardt
2010-06-03  2:36   ` Yannick Duchêne (Hibou57)
2010-06-03  7:57     ` Dmitry A. Kazakov
2010-06-03  8:21       ` Yannick Duchêne (Hibou57)
2010-06-03 16:03     ` Adam Beneschan
2010-06-05 18:47   ` Marc A. Criley
replies disabled

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