comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Why constant components are not permitted ?
Date: Fri, 25 Nov 2011 11:45:51 +0100
Date: 2011-11-25T11:45:51+01:00	[thread overview]
Message-ID: <kygsreqdjpgd.7484fknjlm68$.dlg@40tude.net> (raw)
In-Reply-To: 16f6610a-39e3-4988-a239-eb7c1778ade8@e2g2000vbb.googlegroups.com

On Fri, 25 Nov 2011 02:23:22 -0800 (PST), Ludovic Brenta wrote:

> Dmitry A. Kazakov wrote on comp.lang.ada:
>> On Thu, 24 Nov 2011 20:46:13 +0100, Ludovic Brenta wrote:
>>> Constants with other purposes can be declared outside the type
>>> definition and thus shared among all objects of the declared type.
>>
>> The constant value may depend on the enclosing object, undesired or
>> impossible to re-evaluate each time when needed.
> 
> IOW, the constant is the cached result of a function taking the rest,
> or part thereof, of the object as its sole parameter.

and possibly the context where the object was created.

> If this result
> is a constant, this can only mean that the rest of the object is
> constant too, right?

No. The object itself can be mutable. Otherwise we would need no
discriminants, which among other play the role of constant components.

> If not all other components are inputs into the function returning
> the constant result, then it is probably desirable to split the object
> into two types, such that all the "constant" components are in a type
> of their own:
> 
> type Immutable_Part is record
>    Hash_Value : Natural;
>    Component_1 : Foo;
> end record;
> 
> type T (Immutable : not null access constant Immutable_Part) is record
>    Component_2 : Bar;
> end record;

That would complicate design. If discriminants must keep on playing the
role of constant components, then Ada should allow any objects as
discriminants:

   type T (Immutable : constant Immutable_Part) is record
      Component_2 : Bar;
   end record;

However I would prefer discriminants used as constraints and only
constraints in order to produce constrained subtypes.

Furthermore, since Ada does not have abstract record interface (getter and
setter), you might also with to have this:

   type T is private with record
      Field_1 : constant Integer;  -- You cannot change this
      Field_2 : constant Integer;  -- You cannot change this either
   end record;
private
   type T is record
      Field_1 : constant Integer := 3; -- Nobody can change this field
      Field_2 : Integer; -- I can change this, you cannot
      Field_3 : Integer; -- You cannot see this one
   end record;

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



  reply	other threads:[~2011-11-25 10:46 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-24 18:14 Why constant components are not permitted ? David Sauvage
2011-11-24 19:06 ` anon
2011-11-24 19:49   ` David Sauvage
2011-11-24 22:55     ` Georg Bauhaus
2011-11-24 23:53     ` anon
2011-11-24 19:46 ` Ludovic Brenta
2011-11-25  9:10   ` Dmitry A. Kazakov
2011-11-25 10:23     ` Ludovic Brenta
2011-11-25 10:45       ` Dmitry A. Kazakov [this message]
2011-11-25  6:56 ` Niklas Holsti
2011-11-25  9:53   ` Yannick Duchêne (Hibou57)
2011-11-25 10:06     ` AdaMagica
2011-11-25 10:16       ` AdaMagica
2011-11-25 10:56       ` Yannick Duchêne (Hibou57)
2011-11-25 17:41       ` Niklas Holsti
2011-11-26  0:23         ` anon
2011-11-29  4:10         ` Randy Brukardt
2011-11-29  7:55           ` David Sauvage
2011-11-29 10:55             ` Yannick Duchêne (Hibou57)
2011-11-29 11:17             ` Mark Lorenzen
2011-11-26 10:22       ` Pascal Obry
2011-11-26 10:59         ` Dmitry A. Kazakov
2011-11-25  7:03 ` AdaMagica
2011-11-25  9:12 ` Dmitry A. Kazakov
2011-11-25  9:57   ` Yannick Duchêne (Hibou57)
2011-11-25 10:22     ` Dmitry A. Kazakov
2011-11-25 11:00       ` Yannick Duchêne (Hibou57)
replies disabled

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