From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Unknown constraints and type composition Date: Thu, 14 Jun 2018 19:53:07 +0200 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 14 Jun 2018 17:53:07 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="0ddf80645ebc11bd3b8e5a50b804cdfb"; logging-data="4733"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vPqB8PZRR5hJNpKM4s0Z49801VwSu9PE=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 Cancel-Lock: sha1:mQ32kzfyPfgIsxeubieKXjZm27A= In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:53104 Date: 2018-06-14T19:53:07+02:00 List-Id: On 06/14/2018 05:37 PM, Alejandro R. Mosteo wrote: > I think I have read somewhere that types with unknown constraints are a good way > of ensuring you (or your users) don't end with uninitialized values: > > types Whatever (<>) is [limited] private; > > function Create return Whatever; It's one way. There are others that might be better. Unknown discriminants are more for generic formal types, to show that the generic accepts indefinite actual types. One way to deal with this is to make the full type a record with reasonable defaults for all the components. This works for all versions of the language. Another is to make the type a descendant of [Limited_}Controlled and override Initialize. This works for Ada 95 and later. Another way is to have function Initialized (Thing : Whatever) return Boolean; that returns True if its parameter has been initialized. Have a Dynamic_Predicate on Whatever that Initialized returns True. Have a postcondition on your New_Whatever function that its return value is Initialized. Make the full type a record with an Initialized component default initialized to False. This only works for Ada 12. Another is to leave off the predicate, and instead give all operations on the type the precondition that the value is Initialized. This only works for Ada 12, but it can be emulated in any version of Ada with manual checks of the precondition. > This seems nice at first sight but when these types have any likelihood of > ending as members of another type you will hit the "unconstrained member" problem. After having made an effort to make the type indefinite, you should not be surprised that it's an indefinite type. Part of design is to try to anticipate all reasonable uses for a type, and choose an approach that works for them all. If this is a reasonable use of the type, then unknown discriminants is not a suitable approach. -- Jeff Carter "When Roman engineers built a bridge, they had to stand under it while the first legion marched across. If programmers today worked under similar ground rules, they might well find themselves getting much more interested in Ada!" Robert Dewar 62