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!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!gandalf.srv.welterde.de!news.jacob-sparre.dk!loke.jacob-sparre.dk!pnx.dk!.POSTED!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: storage error: stack overflow Date: Tue, 18 Aug 2015 15:56:04 -0500 Organization: JSA Research & Innovation Message-ID: References: <87r3n0kc15.fsf@adaheads.sparre-andersen.dk> NNTP-Posting-Host: rrsoftware.com X-Trace: loke.gir.dk 1439931365 30799 24.196.82.226 (18 Aug 2015 20:56:05 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Tue, 18 Aug 2015 20:56:05 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 Xref: news.eternal-september.org comp.lang.ada:27496 Date: 2015-08-18T15:56:04-05:00 List-Id: "Jeffrey R. Carter" wrote in message news:mqvejr$hai$2@dont-email.me... > On 08/18/2015 07:02 AM, hreba wrote: >> >> Huh? As much as I know you cannot declare variables of a type with >> unknown >> storage requirements at compile time, such as class-wide types and arrays >> with >> unknown range. > > These are "indefinite types". You can declare an object of an indefinite > type as > long as you supply an initialization expression: > > S : String := "Hello"; > > V : T'Class := Some_Function; And if you need to change the type of the object after initialization, you can use the "Holder" container, which exists for this very purpose: package T_Class_Holder is new Ada.Containers.Indefinite_Holders (T'Class); V : T_Class_Holder.Holder; begin V.Replace_Element (Some_Function); ... V.Replace_Element (Some_Other_Function); ... end; In Ada 2012, you can also use the reference function to modify the object, but you can't change the tag that way. That is: V.Reference := Some_Function; As noted, this will raise Constraint_Error if the tag of V.Reference is changed. I may sound like a broken record (scratched CD?? ;-), but using a Holder rather than explicit access types lets the library do the storage management rather than the programmer. There are much less likely to be bugs that way. And of course, you can do this with all of the containers; if you need a Map or Tree or Vector [including an array] of T'Class, just instantiate the appropriate container and again let someone at your compiler vendor get the storage management right, rather than doing it yourself. Randy.