comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: A bad counterintuitive behaviour of Ada about OO
Date: Tue, 5 Aug 2014 16:25:49 -0700 (PDT)
Date: 2014-08-05T16:25:49-07:00	[thread overview]
Message-ID: <dc61413d-f1c8-46e5-a480-99c0d92343cf@googlegroups.com> (raw)
In-Reply-To: <lrrm86$igr$1@speranza.aioe.org>

On Tuesday, August 5, 2014 3:35:49 PM UTC-7, Victor Porton wrote:

> So now I understand what I should do. To eliminate side effects when copying 
> subobjects of Base_Object type, I should remove overriding Adjust from 
> Base_Object and define it manually for particular object types. Copy_Handle 
> then becomes unnecessary and should be removed altogether.

This seems to be on the right track.  An "Adjust" procedure that destroys data (e.g. by clearing it to 0, as you've done) is asking for trouble.  The problem, as you've found out, is that records could be copied at times when you're not expecting.  The semantics in the LRM for function calls and aggregates talk about creating anonymous objects and then copying them to their final destination, which could involve extra Adjust/Finalize calls.  Then it spells out cases where an implementation is permitted to build stuff in place (instead of building in a temporary location and copying), and avoid an Adjust/Finalize pair, and it spells out other cases where the implementation is *required* to build stuff in place.

I tried your example with two different compilers, and in both cases, the process of calling Handled_Record.From_Handle led to an Adjust call that cleared the data to 0.  This was true even after you added an explicit From_Handle for the Example_Object; your expression still called the Handled_Record.From_Handle function and that led to the Adjust call.  As an experiment, I tried rewriting From_Handle from this:

   function From_Handle(Handle: Handle_Type) return Base_Object is 
   begin 
      return (Ada.Finalization.Controlled with Handle=>Handle); 
   end; 

to this:

   function From_Handle(Handle: Handle_Type) return Base_Object is 
   begin 
      return Result : Base_Object do
          Result.Handle := Handle;
      end return;
   end; 

This caused the program to avoid calling Handled_Record.Adjust with one compiler, thus outputting the expected result, but not with the other compiler.  And neither compiler is wrong.  They're taking advantage of implementation permissions differently.

So you have to be careful when defining Adjust/Finalize for a type.  You have to make sure that whenever you call a function that returns that type, or use an aggregate or extension aggregate of that type, you have to make sure the program will work if one *or* *more* Adjust/Finalize pairs are called when moving the data around from one location to another, and you have to make sure it will work if Adjust/Finalize are *not* called.  Your original Adjust fails that test.

                                 -- Adam

  reply	other threads:[~2014-08-05 23:25 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 [this message]
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
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