comp.lang.ada
 help / color / mirror / Atom feed
From: Eric Hughes <eric.eh9@gmail.com>
Subject: Re: Limited initialization for non-limited types
Date: Tue, 1 Apr 2008 21:06:32 -0700 (PDT)
Date: 2008-04-01T21:06:32-07:00	[thread overview]
Message-ID: <aef8db4a-2050-404d-8e2e-9904fabef4a6@d21g2000prf.googlegroups.com> (raw)
In-Reply-To: b481036a-41ee-4fc5-a0d0-c73cfef60234@s19g2000prg.googlegroups.com

On Mar 26, 7:26 am, I wrote:
>     package Foo is
>             type X is new Ada.Finalization.Controlled with
>             record T : Trace ; end record ;
>         Trace_Stash : Trace ;
>     end Foo ;
> ...
>     package body Foo is
>         procedure Finalize( Object : in out X ) is begin
>             Trace_Stash = Object.T ;
>             Object.T.Trace_Finalize ;
>         end ;
>         procedure Adjust( Object : in out X ) is begin
>             Object.T = Trace_Stash ;
>             Object.T.Trace_Adjust ;
>         end ;
>     end Foo ;

OK, figuring out initialization was blindingly easy once I got the
right trick.  Consider the following additional code in package Foo:

    package Foo
        [...]
        type Nil is null record ;
        function Construct_X( T : Trace ) return Nil ;
    end Foo ;
...
    package body Foo is
        function Construct_X( T : Trace ) return Nil is begin
            Trash_Stash = T ;
            return Nonce : Nil ;
        end ;
        function Initialize( Object : in out X ) is begin
            Object.T = Trace_Stash ;
            Object.T.Trace_Initialize ;
        end ;
     end Foo ;

Why do I have a constructor function for X that returns Nil?  Here's
the sample code:

    declare
        T : Trace ;
        NA : Nil := Construct_X( T ) ;
        A : X ;
    begin
        ...

What happens is that the Nil constructor has a side effect upon a
package variable, which is then used in the default initialization of
variable A.  The realization I needed was that I could use a trivial
constructor function within a declarative_part--where I would have
otherwise used a procedure--to get side effects by which default
initialization would work.  That part of this trick is applicable in
other code.

My actual code is more involved.  Here's an actual snippet, lifted
from a unit test:
   [declarative_part]
      NA : Nil := Construct_Traced_Existence( Name => "A", T => T ) ;
      A : Traced_Existence ;
      NB : Nil := Construct_Traced_Existence( Name => "B", T => T ) ;
      B : Traced_Existence ;
   [handled_sequence_of_statements]
      A := B ;

The trace that comes back (it's generated at run-time) is "(A.I)(B.I)
(A.F)(A.A)", just like you'd expect.  The previous method I had tried,
which used ordinary constructor functions, that one didn't work well
at all.

Eric



      parent reply	other threads:[~2008-04-02  4:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-26 13:26 Limited initialization for non-limited types Eric Hughes
2008-03-26 14:02 ` Robert A Duff
2008-03-27  0:07   ` Eric Hughes
2008-03-26 15:08 ` Dmitry A. Kazakov
2008-03-26 22:13 ` Randy Brukardt
2008-03-27  3:25   ` Eric Hughes
2008-03-28  6:56     ` Randy Brukardt
2008-03-28 15:25       ` Eric Hughes
2008-03-28 21:53         ` Randy Brukardt
2008-03-28 23:37           ` Eric Hughes
2008-04-02  3:00         ` Eric Hughes
2008-03-26 23:00 ` Lucretia
2008-03-28 11:23 ` Martin Krischik
2008-03-28 15:47   ` Eric Hughes
2008-04-02  4:06 ` Eric Hughes [this message]
replies disabled

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