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,FREEMAIL_FROM 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!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: hreba Newsgroups: comp.lang.ada Subject: Re: storage error: stack overflow Date: Wed, 19 Aug 2015 08:56:22 -0300 Message-ID: References: <87r3n0kc15.fsf@adaheads.sparre-andersen.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net HXydCgGGbe3A4h2NEEcqQwbkcZPVU0zTmXh3BBGviYlrPPBrLd Cancel-Lock: sha1:IVkbqFruHpS7wPm8mhXzdGW9D08= User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.8.0 In-Reply-To: Xref: news.eternal-september.org comp.lang.ada:27504 Date: 2015-08-19T08:56:22-03:00 List-Id: On 08/19/2015 05:44 AM, Georg Bauhaus wrote: > On 19.08.15 03:48, hreba wrote: >> My knowledge of Ada exceeds Ada 95 only by very little but I am pretty >> sure that in such a case one uses variables of a class-wide type, and >> as one cannot initialize them one has to declare them as access types. > > The assumption that limited objects cannot be initialized like non-limited > (by sort of copying a value) is no longer true, and it wasn't the whole > truth > in Ada 95. > There are cases when nothing but an access type will do. But due > to build-in-place, limited tagged objects can be declared and initialized, > by aggregate or by function call, using just traditional syntax as > Jeffrey Carter explains. This includes objects of a class-wide type, > > Built-in-place (in situ), perhaps simplifying, means that the limited > object is declared and passed/used behind the scenes, for initialization. > For example, the object that is declared becomes the to-be-returned > object if there is a function call after “:=” in the declaration. > (See "extended return" of Ada 2005.) > > Given > > package Classwide is > > type T0 is abstract tagged limited null record; > > type T1 is new T0 with private; > function Make return T1; > > function Produce (Variant : Boolean) return T1'Class; > > private > type T1 is new T0 with > record -- note default values for "default construction" > Name : Character := '?'; > end record; > end Classwide; > > package body Classwide is > type T2 is new T1 with > record > Count : Natural; > end record; > function Make return T2; > > function Make return T1 is > begin > return T1'(T0 with Name => '*'); > end Make; > > function Make return T2 is > begin > return T2'(T1'(Make) with Count => 0); > end Make; > > function Produce (Variant : Boolean) return T1'Class is > begin > case Variant is > when False => > return T1'(T0 with Name => 'T'); > when True => > return T2'(T0 with Name => 'F', Count => 0); > end case; > end Produce; > end Classwide; > > > Then, the type hierarchy above includes limited T0, T1, and invisible T2. > All of these types can be used to declare or create objects of specific > types and of class-wide types. > > with Classwide; > procedure Main is > use Classwide; > X : T1; > C : T1'Class := Make; > CC : T1'Class := Produce (Variant => False); > procedure Takes_T1C (Object : in T1'Class) is separate; > procedure Mods_T1C (Object : in out T1'Class) is separate; > begin > Takes_T1C (X); > Takes_T1C (C); > Takes_T1C (CC); > Takes_T1C (Make); > Takes_T1C (Produce (Variant => True)); > Mods_T1C (X); > Mods_T1C (C); > Mods_T1C (CC); > end Main; > > The Ada Rationale will illustrate and explain. > Thanks for your explications Jeffrey an Georg. Apparently Barnes: "Programming in Ada 95" is obsolete with respect to this topic (not regarding correctness, just best practices). What didactic text about object oriented programming in Ada would you recommend? -- Frank Hrebabetzky +55 / 48 / 3235 1106 Florianopolis, Brazil