comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Forcing use of "constructors"
Date: Mon, 23 Nov 2009 05:38:03 -0800 (PST)
Date: 2009-11-23T05:38:03-08:00	[thread overview]
Message-ID: <27db03bc-9655-4a65-a730-bcad83bfe616@h2g2000vbd.googlegroups.com> (raw)
In-Reply-To: 15055d2d-0dd5-4c84-bd4a-1cc60e9dea2d@p32g2000vbi.googlegroups.com

xorque wrote:
> Hello, all.
>
> What's the preferred way to mandate that objects be created by
> the use of given "constructor" subprograms?
>
> By this, I mean that I want to create a type and the only way to
> get new values of this type is to use a subprogram that I provide.
>
> * It's a record type.
> * A few of the components must be set to "sensible" values before
>   use.
> * What constitutes a "sensible" value isn't known until runtime.
> * I don't particularly want to add "is initialized" checks on all
>   subprograms in the package.
>
> Regards,
> xw

Declare your type to have unknown discriminants:

package P is
   type T (<>) -- unknown discriminants: objects requires
initialization
      is [limited] private;
   function Construct return T;
private
   type T is [limited] record ... end record;
end P;

If the type is limited, the only way for a client to create an object
is to call Construct. If the type is nonlimited, the client has two
ways: call Construct or copy an existing object to a new one.

Inside Construct, you would normally use the extended return statement
(ARM 6.5(2.1/2)), e.g.

function Construct return T is
begin
   return Object : T do
      -- initialize some components
   end return;
end Construct;

You can also make your type controlled or limited controlled. You can
expose the controlledness in the public part or hide it in the private
part.

--
Ludovic Brenta;



  reply	other threads:[~2009-11-23 13:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-23 11:41 Forcing use of "constructors" xorque
2009-11-23 13:38 ` Ludovic Brenta [this message]
2009-11-23 14:15   ` xorque
2009-11-23 14:20   ` Maciej Sobczak
2009-11-23 16:18   ` Robert A Duff
replies disabled

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