comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Discriminants or Constructor Function for Limited Types
Date: Wed, 4 May 2022 18:49:51 -0500	[thread overview]
Message-ID: <t4v3f0$lgb$1@dont-email.me> (raw)
In-Reply-To: t4ti4a$s1u$1@gioia.aioe.org

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:t4ti4a$s1u$1@gioia.aioe.org...
> On 2022-05-04 11:02, R R wrote:
>> There are two ways (to my knowledge) how to initialize objects of limited 
>> types. Either the limited type has some discriminants
>>
>>     type DLT (Width, Length : Positive) is tagged limited private;
>>     Obj : DLT (3, 5);
>>
>> or I can provide a constructor function that takes corresponding 
>> parameters
>>
>>     type LT (<>) is tagged limited private;
>>     function Make (Width, Length : Positive) return LT;
>>     Obj : LT := Make (3, 5);
>>
>> Do you recommend one way over the other? Why? Is it possible to combine 
>> both methods (discriminants plus constructor)?
>
> Two more ways are allocators and limited aggregates.
>
> There is no good way to safely initialize a limited object because Ada 
> lacks proper constructors and because the initialization model is 
> inherently unsafe with regard or exceptions, task components, 
> self-referential discriminants (AKA Rosen's trick).
>
> But there are numerous hacks and workarounds depending on the objective.

Those cases that you worry are "unsafe" seem to me to only occur because of 
"hacks and workarounds". There's no good reason to do any of those things 
intentionally unless you are using a "hack" to do something dubious in the 
first place.

To answer the OPs question, the reason to prefer one initialization scheme 
over another mainly comes down to how you are going to use the type. If you 
don't expect it to be composed with other types, use whatever is comfortable 
(I ususally use a procedute in such cases, and have the type self-initialize 
to "invalid" so it can't be used until a call is made).

If you need to compose the type with other similar types, it helps to use 
the same initialization mechanism for all (that is, it's easiest to 
initialize discriminants with other enclosing discriminants, and initialize 
via a function from another function).

Also note that you can combine discriminants with the (automatically called) 
controlled type procedure Initialize to do more complex initialization based 
on the discriminants. My preference is to make almost all new types 
controlled, not everyone agrees with that.

                             Randy.


  reply	other threads:[~2022-05-04 23:49 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-04  9:02 Discriminants or Constructor Function for Limited Types R R
2022-05-04  9:40 ` Jeffrey R.Carter
2022-05-04  9:47 ` Dmitry A. Kazakov
2022-05-04 23:49   ` Randy Brukardt [this message]
2022-05-05  6:56     ` Dmitry A. Kazakov
2022-05-06  1:53       ` Randy Brukardt
2022-05-06  8:48         ` Dmitry A. Kazakov
2022-05-07  3:26           ` Randy Brukardt
2022-05-07 14:55             ` Dmitry A. Kazakov
2022-05-08  2:32               ` Randy Brukardt
2022-05-08  8:37                 ` Dmitry A. Kazakov
2022-05-08 17:19                   ` Doctor Who
2022-05-08 18:00                     ` Dmitry A. Kazakov
2022-05-08 18:07                       ` Doctor Who
2022-05-09  8:52                       ` Niklas Holsti
2022-05-09  9:45                         ` Dmitry A. Kazakov
2022-05-09 10:19                       ` Doctor Who
2022-05-09 11:15                         ` Dmitry A. Kazakov
2022-05-09 12:05                           ` Doctor Who
2022-05-09 12:31                             ` Dmitry A. Kazakov
2022-05-10  4:48                   ` Randy Brukardt
2022-05-10  6:18                     ` Dmitry A. Kazakov
2022-05-04 15:05 ` AdaMagica
2022-05-05  9:59 ` R R
replies disabled

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