comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Proposal: Constructors, Assignment [LONG]
Date: Mon, 30 Dec 2002 15:14:34 GMT
Date: 2002-12-30T15:14:34+00:00	[thread overview]
Message-ID: <wccadinsfmt.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: aupdra$8uo6l$1@ID-77047.news.dfncis.de

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

[various stuff about constructors]

You should look at AI-287, which says that aggregates are allowed for
limited types.  Also, AI-318 which allows constructor functions for
limited types.

I don't see any reason to add a new feature.  It seems to me that the
problem is that you can't write a constructor function for a limited
type (i.e., a function that creates a new object local to itself, and
returns it), and you can't initialize limited objects.  So the solution
is simply to remove these restrictions.  To build on your example:

    type Internet_Session is
       abstract new Ada.Finalization.Limited_Controlled with
    record
       Count: Integer;
       Previous : Internet_Session_Ptr; -- In the global list
       Next     : Internet_Session_Ptr; -- of sessions
    end record;
    
    type Session_Ptr is access Internet_Session;

    function Make_Session(Count: Integer) return Internet_Session is
       Result: Internet_Session 
	 := (Count => Count,
	     Previous | Next => <>); -- use default "null"
    begin
       Result.Previous := ...;
       ...
       return Result;
    end Make_Session;

Then clients could initialize stack and heap objects by calling the
constructor function:
    
    This_Session: Internet_Session := Make_Session(Count => 123);
    That_Session: Session_Ptr := new Internet_Session'(Make_Session(123));

The above aggregate is currently illegal, and the "return Result" won't
work because it violates accessibility rules.  And the two initial
values are currently illegal.  The two AI's would allow the above.

I'm glossing over some details that are probably documented either in
the AI's, or in the minutes of some ARG meeting.

The main point is that limited types are *too* limited.  Their purpose
is to prevent *copying* (assignment statements).  But initialization is
*not* like an assignment, it need not involve a copy, and it should be
allowed for limited types.

> The problem is not only the limited types. The problem is more general. It 
> is user-construction/destruction for ALL user-defined types. 

I don't understand that.  Functions work fine as constructors for
non-limited types.  The problem is for limited types.

As for destructors, well Ada.Finalization works OK, although it has some
flaws.  Those flaws seem unrelated to the constructor-for-limited-type
issue.

>...It is 
> user-assignment for ALL non-limited user-defined types.

That seems like a totally unrelated issue.

> 1. Your proposal is based on a bitwise copy[-constructor]...

The whole point of limited types is that they *cannot* be copied.
Access discriminants don't work if copies are made.  Locks in protected
objects don't work.  Various data structures involving pointers don't
work, as your example showed.  Etc.

It is important that the Result object of Make_Session above be built in
place, in its final destination.  It cannot be built on the local stack
frame and then moved elsewhere.  This requires that the compiler pass in
a Storage_Pool object telling it where to allocate.  A special
Storage_Pool could mean "on the stack", but in the That_Session
initialization above, the object must be allocated in the storage pool
belonging to Session_Ptr, which could be user defined.

- Bob



  reply	other threads:[~2002-12-30 15:14 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-24 11:16 Proposal: Constructors, Assignment [LONG] Dmitry A. Kazakov
2002-12-26 22:11 ` Nick Roberts
2002-12-27 17:43   ` Dmitry A. Kazakov
2002-12-27 20:17     ` Randy Brukardt
2002-12-29 13:43       ` Dmitry A. Kazakov
2002-12-29 18:45         ` Nick Roberts
2002-12-30 12:23           ` Dmitry A. Kazakov
2002-12-30 15:14             ` Robert A Duff [this message]
2002-12-31 13:02               ` Dmitry A. Kazakov
2003-01-01  0:28                 ` Randy Brukardt
2003-01-01 14:13                   ` Dmitry A. Kazakov
2003-01-02 19:44                     ` Randy Brukardt
2003-01-03 13:21                       ` Dmitry A. Kazakov
2003-01-03 19:29                         ` Randy Brukardt
2003-01-03 20:50                       ` Robert A Duff
2003-01-04 12:53                         ` Dmitry A. Kazakov
2003-01-01  0:54         ` Randy Brukardt
2003-01-01 14:13           ` Dmitry A. Kazakov
2003-01-02 19:36             ` Randy Brukardt
2003-01-03 13:20               ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox