comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Re: A bad counterintuitive behaviour of Ada about OO
Date: Tue, 12 Aug 2014 00:50:41 -0700 (PDT)
Date: 2014-08-12T00:50:41-07:00	[thread overview]
Message-ID: <6ef86b67-17ae-449f-93d2-7fdf8ce6bd85@googlegroups.com> (raw)
In-Reply-To: <v1q6bffczns2.yysly6razyg3$.dlg@40tude.net>

W dniu poniedziałek, 11 sierpnia 2014 10:23:09 UTC+2 użytkownik Dmitry A. Kazakov napisał:

> > And there is no way to make *both* work in Ada. You can get one or the
> > other but not both.
> 
> Of course there is a way. If you have two operations with different
> profiles you can overload them.

Hm... And then one overloaded version would delegate to another one.
I have to admit that this sounds like a plausible work-around. It requires more scaffolding than in C++ or Java (there are additional functions that exist only for technical reasons and that do not represent any design-level concepts), but it allows seamless integration at the call side, which was the actual goal.

I have learned something from this discussion. For future reference I paste the complete example program that shows the concepts in action:

with Ada.Text_IO;

procedure Test is
   
   package Products is
      
      type Any_Product is interface;
      
      type A_Product is new Any_Product with null record;
      type B_Product is new Any_Product with null record;
      
   end Products;
   
   package Factories is
      
      type Any_Factory is interface;
      function Make (F : in Any_Factory)
                    return Products.Any_Product'Class is abstract;
      
      type A_Factory is new Any_Factory with null record;
      overriding function Make (F : in A_Factory)
                    return Products.Any_Product'Class;
      function Make (F : in A_Factory)
                    return Products.A_Product;
      
      type B_Factory is new Any_Factory with null record;
      overriding function Make (F : in B_Factory)
                    return Products.Any_Product'Class;
      function Make (F : in B_Factory)
                    return Products.B_Product;
      
   end Factories;
   
   package body Factories is
      
      overriding
      function Make (F : in A_Factory)
                    return Products.Any_Product'Class is
      begin
         -- delegates to actual maker:
         return Products.A_Product'(F.Make);
      end Make;
      
      function Make (F : in A_Factory)
                    return Products.A_Product is
         Result : Products.A_Product;
      begin
         Ada.Text_IO.Put_Line ("Making A_Product");
         return Result;
      end Make;
      
      overriding
      function Make (F : in B_Factory)
                    return Products.Any_Product'Class is
      begin
         -- delegates to actual maker:
         return Products.B_Product'(F.Make);
      end Make;
      
      function Make (F : in B_Factory)
                    return Products.B_Product is
         Result : Products.B_Product;
      begin
         Ada.Text_IO.Put_Line ("Making B_Product");
         return Result;
      end Make;
      
   end Factories;
   
   procedure Use_Any_Factory (F : in Factories.Any_Factory'Class) is
      P : Products.Any_Product'Class := F.Make;
   begin
      null;
   end Use_Any_Factory;
   
   A_F : Factories.A_Factory;
   B_F : Factories.B_Factory;
   
begin
   Ada.Text_IO.Put_Line ("Using A_Factory (via class-wide)");
   Use_Any_Factory (A_F);
   
   Ada.Text_IO.Put_Line ("Using B_Factory (via class-wide)");
   Use_Any_Factory (B_F);
   
   Ada.Text_IO.Put_Line ("Using A_Factory (specific)");
   declare
      A_P : Products.A_Product := A_F.Make;
   begin
      null;
   end;
   
   Ada.Text_IO.Put_Line ("Using B_Factory (specific)");
   declare
      B_P : Products.B_Product := B_F.Make;
   begin
      null;
   end;
end Test;

All calls do what I wanted.

-- 
Maciej Sobczak * http://www.msobczak.com * http://www.inspirel.com

  reply	other threads:[~2014-08-12  7:50 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-05 20:09 A bad counterintuitive behaviour of Ada about OO Victor Porton
2014-08-05 20:58 ` Simon Wright
2014-08-05 21:06   ` Victor Porton
2014-08-05 21:51     ` Niklas Holsti
2014-08-05 22:13       ` Victor Porton
2014-08-05 22:35   ` Victor Porton
2014-08-05 23:25     ` Adam Beneschan
2014-08-05 20:59 ` Dmitry A. Kazakov
2014-08-05 21:07   ` Victor Porton
2014-08-05 22:39     ` Shark8
2014-08-05 21:11   ` Victor Porton
2014-08-06  7:26     ` Dmitry A. Kazakov
2014-08-07  7:41       ` Maciej Sobczak
2014-08-07  8:50         ` Dmitry A. Kazakov
2014-08-08  7:54           ` Maciej Sobczak
2014-08-08  8:14             ` Dmitry A. Kazakov
2014-08-08 13:06               ` Maciej Sobczak
2014-08-08 13:22                 ` Dmitry A. Kazakov
2014-08-08 22:32                   ` Randy Brukardt
2014-08-09 16:11                   ` Maciej Sobczak
2014-08-09 16:48                     ` Dmitry A. Kazakov
2014-08-10 20:55                       ` Maciej Sobczak
2014-08-11  7:41                         ` Dmitry A. Kazakov
2014-08-11  7:58                           ` Maciej Sobczak
2014-08-11  8:23                             ` Dmitry A. Kazakov
2014-08-12  7:50                               ` Maciej Sobczak [this message]
2014-08-11 11:35                             ` G.B.
2014-08-08 22:26                 ` Randy Brukardt
2014-08-08  8:34             ` Shark8
2014-08-08 12:59               ` Maciej Sobczak
2014-08-08 22:37                 ` Randy Brukardt
2014-08-08 22:53                   ` Jeffrey Carter
2014-08-07  8:58         ` J-P. Rosen
2014-08-07  9:40           ` Dmitry A. Kazakov
2014-08-07 11:17             ` J-P. Rosen
2014-08-07 12:28               ` Dmitry A. Kazakov
2014-08-07 13:34                 ` J-P. Rosen
2014-08-07 16:10                   ` Dmitry A. Kazakov
2014-08-07 18:14                     ` Robert A Duff
2014-08-07 19:41                       ` Dmitry A. Kazakov
2014-08-07 20:53                         ` Robert A Duff
2014-08-08  7:43                           ` Dmitry A. Kazakov
2014-08-08  8:18                             ` Shark8
2014-08-08  7:45                     ` J-P. Rosen
2014-08-08  8:04                       ` Dmitry A. Kazakov
2014-08-08  8:55                         ` J-P. Rosen
2014-08-08  9:13                           ` Dmitry A. Kazakov
2014-08-08 10:01                             ` J-P. Rosen
2014-08-08 10:53                               ` Dmitry A. Kazakov
2014-08-08 10:56                                 ` Victor Porton
2014-08-08 12:00                                 ` J-P. Rosen
2014-08-08 13:11                                   ` Dmitry A. Kazakov
2014-08-08 13:53                                     ` J-P. Rosen
2014-08-08 20:23                                       ` Dmitry A. Kazakov
2014-08-07 20:29                   ` Shark8
2014-08-08  7:49                     ` J-P. Rosen
2014-08-08  8:12                       ` Shark8
2014-08-08  8:26                         ` Dmitry A. Kazakov
2014-08-08 11:10                           ` Shark8
2014-08-08 11:20                             ` Dmitry A. Kazakov
2014-08-08 19:34                               ` Shark8
2014-08-08 20:23                                 ` Dmitry A. Kazakov
2014-08-07 15:03           ` Jeffrey Carter
2014-08-08  7:48           ` Maciej Sobczak
2014-08-08  8:51             ` J-P. Rosen
2014-08-08 13:25               ` Maciej Sobczak
2014-08-08 13:34                 ` J-P. Rosen
2014-08-08 13:52                   ` Dmitry A. Kazakov
2014-08-08 14:21                     ` J-P. Rosen
2014-08-08 20:23                       ` Dmitry A. Kazakov
2014-08-08 22:08                     ` Randy Brukardt
2014-08-08 22:18                 ` Randy Brukardt
2014-08-06  4:50 ` Per Sandberg
replies disabled

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