comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: operation can be dispatching in only one type
Date: Mon, 23 Nov 2015 10:40:52 -0700
Date: 2015-11-23T10:40:52-07:00	[thread overview]
Message-ID: <n2viun$sb0$1@dont-email.me> (raw)
In-Reply-To: <b4aee46c-7dbb-4906-99c5-f4f8e9876fc6@googlegroups.com>

On 11/23/2015 03:23 AM, Serge Robyns wrote:
> 
> I'm facing an issue with Ada when it comes to use an operation on two tagged types.  It isn't a road blocker and I've found my way around and are not using tagged types for these anymore.  I've now a function that computes the total balance every time.  Another option is to pass the id and then search the map.
>
>    type T_Balance is tagged record
>       Id       : T_Balance_Id;
>       Name     : T_Balance_Name;
>       Balance  : T_Amount;
>    end record;
> 
>    package Balance_Map is new
>      Ada.Containers.Ordered_Maps (Key_Type   => T_Balance_Id,
>                                   Element_Type => T_Balance);
> 
>    type T_Account is tagged record
>       Name             : T_Account_Name;
>       Total_Balance    : T_Money;
>       Balances         : Balance_Map.Map;
>    end record;
> 
> And I want to have an operation increasing a balance and the total in the account.
> The following declaration is refused:
> 
>    procedure Update_Balance
>      (Self   : in out T_Account;
>       AB     : in out T_Balance;
>       Amount : in     T_Money);
> 
> My hope was that it gets dispatched to the right operation on Self which in turns will call the right operation on AB.  But the compiler seems not to agree.

Ada has only single dispatch, so an operation can only be a primitive operation
of a single tagged type (the "controlling" type).

In Ada, neither the parameter name (Self) nor the position of the parameter in
the parameter list determine the type for which an operation of a tagged type
dispatches. Instead, it dispatches on the parameter of a tagged type for which
it is a primitive operation. [A primitive operation is declared in the same
declarative part as the type (usually a pkg spec).]

Since here you have 2 tagged types in the same declarative part, this operation
is primitive for both, which violates the Ada rule. Note that in the standard
containers, type Cursor is not tagged for precisely this reason: They need
operations that take both a container and a cursor as parameters.

The standard approach to this is to put each tagged type in a separate pkg:

package Balances is
   type Balance_Info is ...

   procedure Op (Balance : in out Balance_Info);
end Balances;

with Balances;
package Accounts is
   type Account_Info is ...

   procedure Op
      (Account : in out Account_Info; Balance : in out Balances.Balance_Info);
end Accounts;

Another approach is to make one parameter classwide; then it is primitive for
only one tagged type:

procedure Op
   (Account : in out Account_Info; Balance : in out Balance_Info'Class);

A third possibility is to make the operation non-primitive by putting it in a
different declarative part:

package Non_Primitive is
   procedure Op
      (Account : in out Account_Info; Balance : in out Balance_Info);
end Non_Primitive;

A fourth approach is to make one of the types non-tagged, as with Cursor in the
containers.

A fifth approach is to make all types non-tagged. This approach causes the
fewest problems.

> class account {
> public:
> 	account_id id;
> 	account_name name;
>         money total_balance;
> 	balance_list balances;
> 
> 	account();
> 	virtual ~account();
> 
> 	void update_balance (account_balance& ab, money amount);

This is a primitive operation of type account (to use the Ada terminology)
because it occurs in account's class structure. Ada has a somewhat idiosyncratic
syntax for programming by extension.

> };

-- 
Jeff Carter
"Now look, Col. Batguano, if that really is your name."
Dr. Strangelove
31


  parent reply	other threads:[~2015-11-23 17:40 UTC|newest]

Thread overview: 132+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-23 10:23 operation can be dispatching in only one type Serge Robyns
2015-11-23 11:29 ` Dmitry A. Kazakov
2015-11-23 13:05   ` Serge Robyns
2015-11-23 13:48     ` Dmitry A. Kazakov
2015-11-23 14:16       ` Serge Robyns
2015-11-23 14:59         ` G.B.
2015-11-23 15:52         ` Dmitry A. Kazakov
2015-11-23 17:40 ` Jeffrey R. Carter [this message]
2015-11-24  9:08   ` Serge Robyns
2015-11-24 16:44     ` AdaMagica
2015-11-24 17:09     ` Jeffrey R. Carter
2015-11-24 18:37       ` Serge Robyns
2015-11-24 20:18         ` Jeffrey R. Carter
2015-11-24 20:40           ` Serge Robyns
2015-11-24 20:25       ` Niklas Holsti
2015-11-24 21:48         ` Jeffrey R. Carter
2015-11-25  8:24           ` Dmitry A. Kazakov
2015-11-25 11:22             ` Serge Robyns
2015-11-25 17:38               ` Dmitry A. Kazakov
2015-11-26 11:30                 ` Serge Robyns
2015-11-26 13:14                   ` Dmitry A. Kazakov
2015-11-26 14:27                     ` Serge Robyns
2015-11-26 15:16                       ` J-P. Rosen
2015-11-26 18:27                         ` Serge Robyns
2015-11-26 21:20                           ` J-P. Rosen
2015-11-27  8:37                             ` Dmitry A. Kazakov
2015-11-27 12:58                               ` J-P. Rosen
2015-11-27 13:39                                 ` Dmitry A. Kazakov
2015-11-30 22:22                                   ` Randy Brukardt
2015-12-01  8:46                                     ` Dmitry A. Kazakov
2015-12-01 11:19                                       ` G.B.
2015-12-01 13:56                                         ` Dmitry A. Kazakov
2015-12-01 16:05                                           ` G.B.
2015-12-01 17:58                                             ` Dmitry A. Kazakov
2015-12-02 13:06                                               ` G.B.
2015-12-02 13:31                                                 ` Dmitry A. Kazakov
2015-12-02 19:33                                           ` Randy Brukardt
2015-12-02 19:27                                       ` Randy Brukardt
2015-11-29 17:59                     ` Jacob Sparre Andersen
2015-11-30 22:29                       ` Randy Brukardt
2015-11-25 12:27             ` G.B.
2015-11-25 17:25               ` Dmitry A. Kazakov
  -- strict thread matches above, loose matches on Subject: below --
2009-11-13 20:12 Operation " xorque
2009-11-13 20:34 ` Dmitry A. Kazakov
2009-11-13 20:43   ` xorque
2009-11-13 21:14     ` Dmitry A. Kazakov
2009-11-13 20:44   ` xorque
2009-11-16 17:43 ` Adam Beneschan
2009-11-16 20:28   ` Dmitry A. Kazakov
2009-11-16 20:32     ` Dmitry A. Kazakov
2009-11-16 21:35     ` Adam Beneschan
2009-11-16 22:28       ` Dmitry A. Kazakov
2009-11-17 22:10         ` Adam Beneschan
2009-11-18  9:46           ` Dmitry A. Kazakov
2009-11-18 16:39             ` Adam Beneschan
2009-11-18 19:21               ` Dmitry A. Kazakov
2009-11-19  0:27                 ` Randy Brukardt
2009-11-19  2:11                   ` Robert A Duff
2009-11-19 15:57                     ` Adam Beneschan
2009-11-19 19:39                       ` Robert A Duff
2009-11-19 23:43                         ` Randy Brukardt
2009-11-19  8:50                   ` Dmitry A. Kazakov
2009-11-19 23:54                     ` Randy Brukardt
2009-11-20  8:34                       ` Dmitry A. Kazakov
2009-11-20 10:58                         ` Jean-Pierre Rosen
2009-11-21  6:02                         ` Randy Brukardt
2009-11-21 13:07                           ` Dmitry A. Kazakov
2009-11-22  5:45                         ` xorque
2009-11-22 11:25                           ` Georg Bauhaus
2009-11-22 11:30                             ` xorque
2009-11-22 16:25                             ` Dmitry A. Kazakov
2009-11-22 16:27                               ` xorque
2009-11-22 16:42                                 ` Dmitry A. Kazakov
2009-11-22 16:52                                   ` xorque
2009-11-22 17:41                                     ` Dmitry A. Kazakov
2009-11-22 18:03                                       ` xorque
2009-11-22 18:08                                         ` xorque
2009-11-22 18:28                                         ` Dmitry A. Kazakov
2009-11-22 18:41                                           ` xorque
2009-11-22 21:47                                           ` Robert A Duff
2009-11-23  3:42                                             ` stefan-lucks
2009-11-30 20:36                                               ` Robert A Duff
2009-11-30 23:54                                                 ` (see below)
2009-12-01 12:13                                                 ` Georg Bauhaus
2009-12-01 12:23                                                   ` Georg Bauhaus
2009-12-01 12:44                                                     ` Georg Bauhaus
2009-12-01 13:48                                                   ` Dmitry A. Kazakov
2009-12-01 15:02                                                     ` Georg Bauhaus
2009-12-01 16:18                                                       ` Dmitry A. Kazakov
2009-12-01 17:52                                                         ` Georg Bauhaus
2009-12-01 18:47                                                           ` Dmitry A. Kazakov
2009-12-01 21:53                                                             ` John B. Matthews
2009-12-02  0:32                                                               ` Georg Bauhaus
2009-12-02 11:18                                                                 ` John B. Matthews
2009-12-02 14:29                                                                   ` Jean-Pierre Rosen
2009-12-02 15:35                                                                     ` Georg Bauhaus
2009-12-02  1:13                                                             ` Georg Bauhaus
2009-12-02  9:07                                                               ` Dmitry A. Kazakov
2009-12-02 12:35                                                                 ` John B. Matthews
2009-12-02 13:35                                                                   ` Dmitry A. Kazakov
2009-12-03  5:23                                                                   ` Randy Brukardt
2009-12-03 20:21                                                                     ` John B. Matthews
2009-12-03  5:29                                                                 ` Randy Brukardt
2009-12-03 11:24                                                                   ` Georg Bauhaus
2009-12-03 23:08                                                                     ` Randy Brukardt
2009-12-04  8:52                                                                       ` Dmitry A. Kazakov
2009-12-05  2:45                                                                         ` Randy Brukardt
2009-12-05 10:32                                                                           ` Dmitry A. Kazakov
2009-12-08  0:19                                                                             ` Randy Brukardt
2009-12-08  4:30                                                                               ` stefan-lucks
2009-12-08  9:12                                                                                 ` Dmitry A. Kazakov
2009-12-10  4:09                                                                                   ` Randy Brukardt
2009-12-11  0:10                                                                                 ` Robert A Duff
2009-12-08  9:22                                                                               ` Dmitry A. Kazakov
2009-12-08 10:06                                                                                 ` Georg Bauhaus
2009-12-08 10:23                                                                                   ` Dmitry A. Kazakov
2009-12-08 10:33                                                                                     ` Georg Bauhaus
2009-12-08 10:49                                                                                       ` Dmitry A. Kazakov
2009-12-01 23:51                                                   ` Randy Brukardt
2009-11-23  8:52                                             ` Dmitry A. Kazakov
2009-11-30 20:43                                               ` Robert A Duff
2009-12-01  9:00                                                 ` Dmitry A. Kazakov
2009-12-01  5:45                                                   ` stefan-lucks
2009-12-01 11:12                                                     ` Dmitry A. Kazakov
2009-12-01  8:01                                                       ` stefan-lucks
2009-12-01 13:37                                                         ` Dmitry A. Kazakov
2009-12-15 23:54                                                         ` Robert A Duff
2009-11-23  7:48                                         ` Georg Bauhaus
2009-11-23  7:58                                           ` Georg Bauhaus
2009-11-19 16:04                 ` Adam Beneschan
2009-11-19  2:23           ` tmoran
2009-11-19  8:32             ` Dmitry A. Kazakov
replies disabled

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