comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Inherited Methods and such
Date: Fri, 28 Sep 2007 15:42:09 -0400
Date: 2007-09-28T15:42:09-04:00	[thread overview]
Message-ID: <wcchclefuge.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: wccps02fw9v.fsf@shell01.TheWorld.com

Robert A Duff <bobduff@shell01.TheWorld.com> writes:

> Maciej Sobczak <see.my.homepage@gmail.com> writes:
>> Constructor functions in Ada are functions that return new objects
>> (most useful for limited types since Ada 2005) can have parameters,
>> but they still don't acknowledge the "progressive" nature of the whole
>> process.
>
> I think they do, actually.

Here's an example.  What do you think?  Do you revise your opinion that
Ada doesn't properly support constructors?

We've got a Root_Window type, and a type derived from that,
for "red" windows (OK, I didn't bother with something more
realistic).

Note that this example dispatches during construction,
and dispatches the way C++ constructors would (shallow,
not deep).  But I think we both agree that dispatching
at all during construction is questionable.

(There's an AI on this issue, by the way.)

Maybe Root_Window should have unknown discriminants (<>),
to force the use of the provided constructors.

I get this output, using the latest (internal to AdaCore) version of
GNAT:

Evilly dispatching from Make_Root_Window.
Hello from Class_Wide
Hello from Primitive (Root_Window)
Goodbye from Class_Wide
Evilly dispatching from Make_Red_Window.
Hello from Class_Wide
Hello from Primitive (Red_Window)
Goodbye from Class_Wide
Hello from Class_Wide
Hello from Primitive (Red_Window)
Goodbye from Class_Wide

And here's the code:

with Ada.Text_IO; use Ada.Text_IO;
package Root_Windows is

   type Root_Window is tagged limited private;

   procedure Primitive (Win : Root_Window);
   procedure Class_Wide (Win : Root_Window'Class);

private

   type Root_Window is tagged limited
      record
         X : Integer;
      end record;

end Root_Windows;

package Root_Windows.Factory is

   function Make_Root_Window (X : Integer) return Root_Window;

end Root_Windows.Factory;

with Ada.Text_IO; use Ada.Text_IO;
with Root_Windows; use Root_Windows;
package Red_Windows is

   type Red_Window is new Root_Window with private;

   procedure Primitive (Win : Red_Window);

private

   type Red_Window is new Root_Window with
      record
         Y : Integer;
      end record;

end Red_Windows;

package Red_Windows.Factory is

   function Make_Red_Window (X, Y : Integer) return Red_Window;

end Red_Windows.Factory;

package body Root_Windows is

   procedure Primitive (Win : Root_Window) is
   begin
      Put_Line ("Hello from Primitive (Root_Window)");
   end Primitive;

   procedure Class_Wide (Win : Root_Window'Class) is
   begin
      Put_Line ("Hello from Class_Wide");
      Primitive (Win); -- dispatch
      Put_Line ("Goodbye from Class_Wide");
   end Class_Wide;

end Root_Windows;

package body Root_Windows.Factory is

   function Make_Root_Window (X : Integer) return Root_Window is
   begin
      return Result : Root_Window := (X => X) do
         Put_Line ("Evilly dispatching from Make_Root_Window.");
         Class_Wide (Result);
      end return;
   end Make_Root_Window;

end Root_Windows.Factory;

package body Red_Windows is

   procedure Primitive (Win : Red_Window) is
   begin
      Put_Line ("Hello from Primitive (Red_Window)");
   end Primitive;

end Red_Windows;

with Root_Windows.Factory;
package body Red_Windows.Factory is

   function Make_Red_Window (X, Y : Integer) return Red_Window is
   begin
      return Result : Red_Window :=
        (Root_Windows.Factory.Make_Root_Window (X) with Y => Y) do

         Put_Line ("Evilly dispatching from Make_Red_Window.");
         Class_Wide (Result);
      end return;
   end Make_Red_Window;

end Red_Windows.Factory;

with Red_Windows.Factory;
procedure Red_Windows.Test is
   My_Window : Root_Window'Class := Factory.Make_Red_Window (X => 1, Y => 2);
begin
   Class_Wide (My_Window);
end Red_Windows.Test;




  reply	other threads:[~2007-09-28 19:42 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-17 14:26 Inherited Methods and such shaunpatterson
2007-09-17 15:11 ` Ludovic Brenta
2007-09-17 16:46   ` shaunpatterson
2007-09-17 19:07     ` Ludovic Brenta
2007-09-17 20:22   ` Maciej Sobczak
2007-09-17 21:07     ` Ludovic Brenta
2007-09-18 14:27       ` Maciej Sobczak
2007-09-18 14:27       ` Maciej Sobczak
2007-09-18 15:25         ` Dmitry A. Kazakov
2007-09-18 18:34           ` Ludovic Brenta
2007-09-18 19:29             ` Dmitry A. Kazakov
2007-09-18 19:39               ` Ludovic Brenta
2007-09-18 20:49                 ` Dmitry A. Kazakov
2007-09-18 21:10               ` Simon Wright
2007-09-18 20:39           ` Maciej Sobczak
2007-09-18 21:12             ` Dmitry A. Kazakov
2007-09-19 14:49               ` Maciej Sobczak
2007-09-19 15:16                 ` Dmitry A. Kazakov
2007-09-19 22:13                   ` Maciej Sobczak
2007-09-20  8:12                     ` Dmitry A. Kazakov
2007-09-20 13:52                       ` Maciej Sobczak
2007-09-20 16:22                         ` Dmitry A. Kazakov
2007-09-20 20:45                           ` Maciej Sobczak
2007-09-21 18:59                             ` Dmitry A. Kazakov
2007-09-21 21:02                               ` Maciej Sobczak
2007-09-22  8:48                                 ` Dmitry A. Kazakov
2007-09-22 21:53                                   ` Maciej Sobczak
2007-09-23  8:41                                     ` Dmitry A. Kazakov
2007-09-23 20:36                                       ` Maciej Sobczak
2007-09-24  9:32                                         ` Dmitry A. Kazakov
2007-09-24 15:02                                           ` Maciej Sobczak
2007-09-24 19:20                                             ` Dmitry A. Kazakov
2007-09-25 20:53                                               ` Maciej Sobczak
2007-09-26 10:42                                                 ` Dmitry A. Kazakov
2007-09-26 21:31                                                   ` Maciej Sobczak
2007-09-27 15:02                                                     ` Dmitry A. Kazakov
2007-09-27 21:02                                                       ` Maciej Sobczak
2007-09-26 12:21                                                 ` Robert A Duff
2007-09-26 12:54                                                   ` Dmitry A. Kazakov
2007-09-26 21:37                                                   ` Maciej Sobczak
2007-09-26 23:47                                                     ` Randy Brukardt
2007-09-27 21:08                                                       ` Maciej Sobczak
2007-09-28  0:44                                                         ` Randy Brukardt
2007-09-28 20:32                                                           ` Maciej Sobczak
2007-09-28 22:35                                                             ` Randy Brukardt
2007-09-29 23:58                                                             ` Robert A Duff
2007-09-26 12:26                                                 ` Robert A Duff
2007-09-26 21:50                                                   ` Maciej Sobczak
2007-09-26 22:20                                                     ` Ray Blaak
2007-09-27  0:01                                                     ` Randy Brukardt
2007-09-27 13:39                                                     ` Robert A Duff
2007-09-27 14:54                                                       ` Dmitry A. Kazakov
2007-09-28  0:35                                                         ` Randy Brukardt
     [not found]                                                           ` <7p6gc1s9imfa$.kmvwf5zyf8e9.dlg@40tude.net>
2007-09-28 22:53                                                             ` Randy Brukardt
2007-09-29 20:37                                                               ` Dmitry A. Kazakov
2007-09-27 21:23                                                       ` Maciej Sobczak
2007-09-28 19:12                                                         ` Robert A Duff
2007-09-28 19:02                                                     ` Robert A Duff
2007-09-28 19:42                                                       ` Robert A Duff [this message]
2007-09-28 20:44                                                         ` Maciej Sobczak
2007-09-28 22:40                                                           ` Randy Brukardt
2007-09-29 20:35                                                           ` Dmitry A. Kazakov
2007-09-29 20:52                                                             ` Maciej Sobczak
2007-09-30  8:38                                                               ` Dmitry A. Kazakov
2007-09-29 23:47                                                             ` Robert A Duff
2007-09-29 20:48                                                           ` Maciej Sobczak
2007-09-29 23:39                                                             ` Robert A Duff
2007-09-30  8:38                                                               ` Dmitry A. Kazakov
2007-09-29 23:42                                                           ` Robert A Duff
2007-09-25  1:59                                   ` Randy Brukardt
2007-09-25  8:59                                     ` Dmitry A. Kazakov
2007-09-25 21:02                                       ` Randy Brukardt
2007-09-26 12:42                                         ` Dmitry A. Kazakov
2007-09-18  4:03 ` Steve
replies disabled

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