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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,27502d015551c708 X-Google-Attributes: gidfac41,public X-Google-Thread: 109fba,2f84446733b06aca X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,2f84446733b06aca X-Google-Attributes: gid103376,public From: "Norman H. Cohen" Subject: Re: Constructors for Ada (was: Converting C++ class to Ada) Date: 1996/12/13 Message-ID: <32B17EFC.4BC7@watson.ibm.com>#1/1 X-Deja-AN: 203925809 references: <32ADF183.7195@lmtas.lmco.com> <58npck$iku$1@goanna.cs.rmit.edu.au> content-type: text/plain; charset=us-ascii organization: IBM Thomas J. Watson Research Center mime-version: 1.0 reply-to: ncohen@watson.ibm.com newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.eiffel x-mailer: Mozilla 3.0 (Win95; I) Date: 1996-12-13T00:00:00+00:00 List-Id: Matthew Heaney wrote: > The issue I have with the language is that there is an asymmetry between > how stack objects are initialized and how heap objects are initialized. > > For example, to initialize an object on the heap: > > type Integer_Access is access Integer; > O : Integer_Access := new Integer'(4); > > Why shouldn't I be able to do that for stack objects too? > > O : Integer'(4); > > What I'd like (and Dale too, it seems) is a way to initialize objects (on > the stack or heap) by invoking a subprogram explicitly identified as a > constructor, and be able to do so irrespective of whether the type is > limited or non-limited. > > The problem is that in Ada initialization of an object on the stack > requires assignment, but this not need be so. It's just a syntactic quirk > that prevents me from calling an initialization subprogram during > elaboration of a limited object. I don't understand Matthew's point. Why is the syntax O : Integer'(4); in any way preferable to O : Integer := 4; ? After all, if LT is a limited type, it cannot be initialized in either an object declaration or an allocator. That is, new LT'(X); is every bit as illegal as O : LT := X; and for very good reason: It entails making a copy of X. Matthew suggests that it would be useful to specify parameters in the declaration of a limited object to control how the object is initialized. Since it is already possible to force a limited object to have a default initial state, and to change the object's state by calling a procedure declared in the same package as the limited type, I see no compelling need for this. (Furthermore, as stated in a previous message, the same effect can often be achieved by deriving the limited type from Limited_Controlled and giving the type discriminants that are read by the type's Initialize procedure. > But suppose my type were limited. For example, I'd like to open a file > during elaboration of the file object: > > F : File_Type'(Name => "my_file.dat", Mode => In_File); > > That's something I can't do now, because type File_Type is limited private. > So I have to do this > > F : File_Type; > begin > Open (F, Name => "my_file.dat", Mode => In_File); What's so bad about with that? -- Norman H. Cohen mailto:ncohen@watson.ibm.com http://www.research.ibm.com/people/n/ncohen