comp.lang.ada
 help / color / mirror / Atom feed
* [HELP] Why is 'Adjust' not called here ???
@ 2002-02-05 10:48 Didier Pieroux
  2002-02-05 16:03 ` Pat Rogers
  2002-02-14 20:05 ` [HELP] " Tucker Taft
  0 siblings, 2 replies; 7+ messages in thread
From: Didier Pieroux @ 2002-02-05 10:48 UTC (permalink / raw)


Hello,

To make it short: why is 'Adjust' not called during initialization by a
aggregate ?

I give an example below illustrating my question.  'A.Instance' is a
controlled type with Initialize, Adjust and Finalize redefined to print
their name. 

I thought that the elaboration of

   Item1 : A.Instance := (Ada.Finalization.Controlled with 10);

would call the 'A.Adjust' procedure... It is not the case. However,
'A.Adjust' is called as expected after the elaboration of

   Item2 : A.Instance := Item1;

I check that behavior with both the Gnat and Aonix compiler.  So, I am
clearly missing something here, and I can't find the answer in my books
(Barnes, Cohen, Ben-Ari and the RM) 

Also, could someone
- Explain me the rational for not calling 'Adjust' in the first case ?
- Give me the key reference(s) in the RM from which this behavior can be
deduced ?

Thank in advance for the light !
Didier



== a.ads =============================================================

with Ada.Finalization;

package A is
   type Instance is new Ada.Finalization.Controlled with
      record
         N: Integer;
      end record;

   procedure Initialize(Self: in out Instance);
   procedure Adjust    (Self: in out Instance);
   procedure Finalize  (Self: in out Instance);
end A;

== a.adb =============================================================

with Ada.Text_Io;
use Ada.Text_Io;

package body A is
   procedure Initialize(Self: in out Instance) is
   begin
      Put_Line("Init");
   end;

   procedure Adjust(Self: in out Instance) is
   begin
      Put_Line("Adjust");
   end;

   procedure Finalize(Self: in out Instance) is
   begin
      Put_Line("Finalize");
   end;
end A;

== my_test.adb =======================================================

with Ada.Finalization;
with A;

procedure My_Test is
   Item1 : A.Instance := (Ada.Finalization.Controlled with 10);
   Item2 : A.Instance := Item1;
begin
   null;
end My_Test;

== Result: ===========================================================

~/Ada/test > gnatmake my_test.adb
gnatgcc -c my_test.adb
gnatgcc -c a.adb
gnatbind -x my_test.ali
gnatlink my_test.ali
~/Ada/test > my_test
Adjust      <-- this corresponds to the Item2 elaboration.
Finalize
Finalize
~/Ada/test >        

-- 
     _________________________________________________________________ 

     Didier Pieroux
     Theoretical Nonlinear Optics, CP 231
     Physics Department, Universite Libre de Bruxelles
     Bvd du Triomphe, B-1050 Brussels, Belgium

     Phone: ++ 32 2 650 5903, Fax: ++ 32 2 650 5824
     dpieroux@hotmail.com
     _________________________________________________________________



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [HELP] Why is 'Adjust' not called here ???
  2002-02-05 10:48 [HELP] Why is 'Adjust' not called here ??? Didier Pieroux
@ 2002-02-05 16:03 ` Pat Rogers
  2002-02-05 16:16   ` Stephen Leake
  2002-02-14 20:05 ` [HELP] " Tucker Taft
  1 sibling, 1 reply; 7+ messages in thread
From: Pat Rogers @ 2002-02-05 16:03 UTC (permalink / raw)


"Didier Pieroux" <dpieroux@pi.be> wrote in message
news:3C5FB880.6FD36A12@hotmail.com...
> Hello,
>
> To make it short: why is 'Adjust' not called during initialization by a
> aggregate ?
>
> I give an example below illustrating my question.  'A.Instance' is a
> controlled type with Initialize, Adjust and Finalize redefined to print
> their name.
>
> I thought that the elaboration of
>
>    Item1 : A.Instance := (Ada.Finalization.Controlled with 10);
>
> would call the 'A.Adjust' procedure... It is not the case. However,
> 'A.Adjust' is called as expected after the elaboration of
>
>    Item2 : A.Instance := Item1;

It is because the first case is initialization via an aggregate, but the second
case (immediately above) is not via an aggregate.


> I check that behavior with both the Gnat and Aonix compiler.  So, I am
> clearly missing something here, and I can't find the answer in my books
> (Barnes, Cohen, Ben-Ari and the RM)
>
> Also, could someone
> - Explain me the rational for not calling 'Adjust' in the first case ?

See below.

> - Give me the key reference(s) in the RM from which this behavior can be
> deduced ?

RM 7.6 {17.1/1}  {For an aggregate of a controlled type whose value is assigned,
other
than by an assignment_statement or a return_statement, the implementation
shall not create a separate anonymous object for the aggregate. The aggregate
value shall be constructed directly in the target of the assignment operation
and Adjust is not called on the target object.}

17.h.1/1 Reason: This is necessary to prevent elaboration problems with deferred
constants of controlled types. Consider:

package P is
   type Dyn_String is private;
   Null_String : constant Dyn_String;
   ...
private
   type Dyn_String is new Ada.Finalization.Controlled with ...
   procedure Finalize(X : in out Dyn_String);
   procedure Adjust(X : in out Dyn_String);

   Null_String : constant Dyn_String :=
      (Ada.Finalization.Controlled with ...);
   ...
end P;

17.h.3/1 When Null_String is elaborated, the bodies of Finalize and Adjust
clearly have not been elaborated. Without this rule, this declaration would
necessarily raise Program_Error (unless the permissions given below are used by
the compiler).


---
Patrick Rogers                       Consulting and Training in:
http://www.classwide.com          Real-Time/OO Languages
progers@classwide.com               Hard Deadline Schedulability Analysis
(281)648-3165                                 Software Fault Tolerance





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [HELP] Why is 'Adjust' not called here ???
  2002-02-05 16:03 ` Pat Rogers
@ 2002-02-05 16:16   ` Stephen Leake
  2002-02-05 17:07     ` Pat Rogers
  0 siblings, 1 reply; 7+ messages in thread
From: Stephen Leake @ 2002-02-05 16:16 UTC (permalink / raw)


> "Didier Pieroux" <dpieroux@pi.be> wrote in message
> news:3C5FB880.6FD36A12@hotmail.com...
> > Hello,
> >
> > To make it short: why is 'Adjust' not called during initialization by a
> > aggregate ?

Because the RM says so :). Pat Rogers gave the RM references.

I believe the rationale is that you can only do initialization by
aggregate in the body of the package defining the type, so you are
assumed to know what you are doing, and should do the equivalent of
the Adjust processing while building the aggregate.

Hmm, what I said about "body of the package" is strictly true only for
limited controlled types. Still, if you have visibility so you can
build an aggregate, you are presumed to know what you are doing.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [HELP] Why is 'Adjust' not called here ???
  2002-02-05 16:16   ` Stephen Leake
@ 2002-02-05 17:07     ` Pat Rogers
  2002-02-06 10:01       ` [Thanks] " Didier Pieroux
  0 siblings, 1 reply; 7+ messages in thread
From: Pat Rogers @ 2002-02-05 17:07 UTC (permalink / raw)


"Stephen Leake" <stephen.a.leake.1@gsfc.nasa.gov> wrote in message
news:uofj3ucng.fsf@gsfc.nasa.gov...
> > "Didier Pieroux" <dpieroux@pi.be> wrote in message
> > news:3C5FB880.6FD36A12@hotmail.com...
> > > Hello,
> > >
> > > To make it short: why is 'Adjust' not called during initialization by a
> > > aggregate ?
>
> Because the RM says so :). Pat Rogers gave the RM references.
>
> I believe the rationale is that you can only do initialization by
> aggregate in the body of the package defining the type, so you are
> assumed to know what you are doing, and should do the equivalent of
> the Adjust processing while building the aggregate.
>
> Hmm, what I said about "body of the package" is strictly true only for
> limited controlled types. Still, if you have visibility so you can
> build an aggregate, you are presumed to know what you are doing.


The AARM gives the reason, though it is quite "pedestrian":

17.h.1/1 Reason: This is necessary to prevent elaboration problems with deferred
constants of controlled types. Consider:

package P is
   type Dyn_String is private;
   Null_String : constant Dyn_String;
   ...
private
   type Dyn_String is new Ada.Finalization.Controlled with ...
   procedure Finalize(X : in out Dyn_String);
   procedure Adjust(X : in out Dyn_String);

   Null_String : constant Dyn_String :=
      (Ada.Finalization.Controlled with ...);
   ...
end P;

17.h.3/1 When Null_String is elaborated, the bodies of Finalize and Adjust
clearly have not been elaborated. Without this rule, this declaration would
necessarily raise Program_Error (unless the permissions given below are used by
the compiler).






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Thanks] Why is 'Adjust' not called here ???
  2002-02-05 17:07     ` Pat Rogers
@ 2002-02-06 10:01       ` Didier Pieroux
  0 siblings, 0 replies; 7+ messages in thread
From: Didier Pieroux @ 2002-02-06 10:01 UTC (permalink / raw)


Pat Rogers wrote:

> > Because the RM says so :). Pat Rogers gave the RM references.


Dear Pat and Stephen,

Thanks to you !

By the way, by reading your answers I discovered that the reason for me
not finding the correct reference was that I was using the "On-line
version of the Ada 95 Rationale at AdaPower.com" which is clearly
missing the "Implementation Requirements" you refer to.  So I guess that
it is not up to date and misses the last corrections. Hough !

Now, I know it is better to consult
"www.ada-auth.org/arm-html/RM-TOC.html" instead ;-)

Best regards,
Didier


-- 
     _________________________________________________________________ 

     Didier Pieroux
     Theoretical Nonlinear Optics, CP 231
     Physics Department, Universite Libre de Bruxelles
     Bvd du Triomphe, B-1050 Brussels, Belgium

     Phone: ++ 32 2 650 5903, Fax: ++ 32 2 650 5824
     http://www.ulb.ac.be/sciences/ont/en.html 
     _________________________________________________________________



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Thanks] Why is 'Adjust' not called here ???
@ 2002-02-06 11:23 Christoph Grein
  0 siblings, 0 replies; 7+ messages in thread
From: Christoph Grein @ 2002-02-06 11:23 UTC (permalink / raw)


> By the way, by reading your answers I discovered that the reason for me
> not finding the correct reference was that I was using the "On-line
> version of the Ada 95 Rationale at AdaPower.com" which is clearly
> missing the "Implementation Requirements" you refer to.  So I guess that
> it is not up to date and misses the last corrections. Hough !
> 
> Now, I know it is better to consult
> "www.ada-auth.org/arm-html/RM-TOC.html" instead ;-)

Please note that the "Ada 95 Rationale" is just that, the "rationale" about the 
Reference Manual, it is _not_ the "Reference Manual" itself.

The Rationale _is_ up to date.

I do not know whether the URL above is the correct one for "Reference Manual 
with Technical Corrigendum 1". Watch out. The latter is definitely somewhere at 
that site.

HTH



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [HELP] Why is 'Adjust' not called here ???
  2002-02-05 10:48 [HELP] Why is 'Adjust' not called here ??? Didier Pieroux
  2002-02-05 16:03 ` Pat Rogers
@ 2002-02-14 20:05 ` Tucker Taft
  1 sibling, 0 replies; 7+ messages in thread
From: Tucker Taft @ 2002-02-14 20:05 UTC (permalink / raw)


Didier Pieroux wrote:
> 
> Hello,
> 
> To make it short: why is 'Adjust' not called during initialization by a
> aggregate ?

We debated this during the Ada 95 design process.  We decided that
Adjust is called after *copying* an existing value, Initialize is
called to create a (new) value by default initialization, and an aggregate
may be used to create a new value exactly as you want it to end up.
Calling Adjust after evaluating the aggregate could be confusing.
Of course, we could have done it otherwise, and required that
when you write an aggregate, you keep in mind that Adjust is
going to be applied to it.

The second issue is whether an aggregate used to initialize an
object should be built in a temporary and then copied into
the object, or built "in place."  There are many advantages
to requiring that the aggregate be built "in place."  As mentioned
elsewhere, it allows you to initialize constants *before* the
Adjust and Finalize procedures have been elaborated.  Secondly,
it avoids the need to allocate space for the temp, copy the
temp into the declared object, adjust the declared object,
finalize the temp, and deallocate the temp.  Of course allocation/deallocation
might be cheap due to being on the stack, but the copy, adjust, and finalize
could be expensive.  The original Ada 95 RM didn't require that you
build the aggregate in place, but a subsequent "Ada Interpretation" (AI-83)
has made this an implementation requirement, to allow portable
programs to declare and initialize constants with aggregates.

If you want to read AI-83, go to:

   http://www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs/AI-00083.TXT?rev=1.16


> 
> I give an example below illustrating my question.  'A.Instance' is a
> controlled type with Initialize, Adjust and Finalize redefined to print
> their name.
> 
> I thought that the elaboration of
> 
>    Item1 : A.Instance := (Ada.Finalization.Controlled with 10);
> 
> would call the 'A.Adjust' procedure... It is not the case. However,
> 'A.Adjust' is called as expected after the elaboration of
> 
>    Item2 : A.Instance := Item1;
> 
> I check that behavior with both the Gnat and Aonix compiler.  So, I am
> clearly missing something here, and I can't find the answer in my books
> (Barnes, Cohen, Ben-Ari and the RM)
> 
> Also, could someone
> - Explain me the rational for not calling 'Adjust' in the first case ?
> - Give me the key reference(s) in the RM from which this behavior can be
> deduced ?
> 
> Thank in advance for the light !
> Didier
>...
>      _________________________________________________________________
> 
>      Didier Pieroux
>      Theoretical Nonlinear Optics, CP 231
>      Physics Department, Universite Libre de Bruxelles
>      Bvd du Triomphe, B-1050 Brussels, Belgium
> 
>      Phone: ++ 32 2 650 5903, Fax: ++ 32 2 650 5824
>      dpieroux@hotmail.com
>      _________________________________________________________________

-- 
-Tucker Taft   stt@avercom.net   http://www.avercom.net
Chief Technology Officer, AverCom Corporation (A Titan Company) 
Bedford, MA  USA (AverCom was formerly the Commercial Division of AverStar:
http://www.averstar.com/~stt)



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2002-02-14 20:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-05 10:48 [HELP] Why is 'Adjust' not called here ??? Didier Pieroux
2002-02-05 16:03 ` Pat Rogers
2002-02-05 16:16   ` Stephen Leake
2002-02-05 17:07     ` Pat Rogers
2002-02-06 10:01       ` [Thanks] " Didier Pieroux
2002-02-14 20:05 ` [HELP] " Tucker Taft
  -- strict thread matches above, loose matches on Subject: below --
2002-02-06 11:23 [Thanks] " Christoph Grein

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