From: "Norman H. Cohen" <ncohen@watson.ibm.com>
Subject: Re: Constructors for Ada (was: Converting C++ class to Ada)
Date: 1996/12/13
Date: 1996-12-13T00:00:00+00:00 [thread overview]
Message-ID: <32B17EFC.4BC7@watson.ibm.com> (raw)
In-Reply-To: mheaney-ya023280001212962345240001@news.ni.net
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
next prev parent reply other threads:[~1996-12-13 0:00 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-12-10 0:00 Converting C++ class to Ada Ken Garlington
1996-12-11 0:00 ` Jon S Anthony
1996-12-12 0:00 ` Ken Garlington
1996-12-18 0:00 ` Jon S Anthony
1996-12-18 0:00 ` Matthew Heaney
1996-12-19 0:00 ` Robert A Duff
1996-12-20 0:00 ` Stephen Leake
1996-12-20 0:00 ` Robert A Duff
1996-12-20 0:00 ` Norman H. Cohen
1996-12-20 0:00 ` Jon S Anthony
1996-12-20 0:00 ` Mitch Gart
1996-12-21 0:00 ` Jon S Anthony
1996-12-11 0:00 ` Norman H. Cohen
1996-12-12 0:00 ` Jon S Anthony
1996-12-11 0:00 ` Larry Kilgallen
1996-12-11 0:00 ` Stephen Leake
1996-12-13 0:00 ` Stephen Leake
1996-12-12 0:00 ` Dale Stanbrough
1996-12-12 0:00 ` Constructors for Ada (was: Converting C++ class to Ada) Matthew Heaney
1996-12-13 0:00 ` Norman H. Cohen [this message]
1996-12-13 0:00 ` Matthew Heaney
1996-12-17 0:00 ` Robert I. Eachus
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox