comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Default value for a record component
Date: Sun, 22 Jul 2007 10:08:16 +0200
Date: 2007-07-22T10:08:02+02:00	[thread overview]
Message-ID: <s8742iqtjqhy.1ap5zo7kmvx01.dlg@40tude.net> (raw)
In-Reply-To: 1185052757.500324.16860@22g2000hsm.googlegroups.com

On Sat, 21 Jul 2007 14:19:17 -0700, Maciej Sobczak wrote:

> Consider:
> 
> package P is
>    type T is record
>       V : Integer := 5;
>    end record;
> end P;
> 
> Above, T.V will have 5 as a default value whenever the instance of T
> is created without any explicit initialization.
> 
> I would like extend it and do this:
> 
> package P is
>    type T is record
>       V : Integer := Get_Default_Value;
>    end record;
> private
>    function Get_Default_Value return Integer;
> end P;
> 
> without losing any of the properties of the original solution and
> without "leaking" any implementation detail to the public view. The
> idea is, of course, to compute the default value at run-time, each
> time the new object is created - but without touching the general
> "look&feel" of the original code (so that for example Controlled is
> excluded).
> Is it possible?

Yes:

package P is
   function Get_Default_Value return Integer;
   type T is record
      V : Integer := Get_Default_Value;
   end record;
end P;

or even like this:

package P is
   Default_Value : Integer;
   type T is record
      V : Integer := Default_Value;
   end record;
end P;

The only problem with this design is that whatever things you wished to
pass to an instance of T, they will be enclosed in P (or it parents). This
is not a "closure." You could achieve a sort of closure, if you made P
generic and passed Get_Default_Value as a formal parameter. But that would
require an instantiation of P each time you wanted to change the things.

In Ada 2005 you can come a bit closer to closures:

package P is
   type T (Default_Value : access function return Integer) is limited
   record
      V : Integer := Default_Value.all;
   end record;
end P;

This looks awful because of pointers. There is still no true procedural
types in Ada, alas. Because of pointers, T has to be limited. Yet it is
almost closure and P can be pure:

   function Get_Default_Value return Integer;
   ...
   X : T (Get_Default_Value'Access);

Feels much better than:

   X : T := (V => Get_Default_Value);

!? (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2007-07-22  8:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-21 21:19 Default value for a record component Maciej Sobczak
2007-07-22  8:08 ` Dmitry A. Kazakov [this message]
2007-07-22 19:44   ` Maciej Sobczak
2007-07-22 21:44     ` Robert A Duff
2007-07-23 19:17       ` Maciej Sobczak
2007-07-23 19:41         ` Dmitry A. Kazakov
2007-07-22 20:01   ` Jeffrey R. Carter
2007-07-24  1:27 ` Randy Brukardt
2007-07-24  9:54   ` Maciej Sobczak
2007-07-24 19:12     ` Randy Brukardt
replies disabled

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