comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nickroberts@blueyonder.co.uk>
Subject: Re: Proposal: Constructors, Assignment [LONG]
Date: Sun, 29 Dec 2002 18:45:21 -0000
Date: 2002-12-29T18:45:21+00:00	[thread overview]
Message-ID: <aunfs4$8e52r$1@ID-25716.news.dfncis.de> (raw)
In-Reply-To: aumu4o$76buu$1@ID-77047.news.dfncis.de

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message
news:aumu4o$76buu$1@ID-77047.news.dfncis.de...

> > -- You'd need to carefully explain why solutions based on functions (as
> > Nick Roberts described) are not sufficient;
>
> They cannot work when no assignment exists. Otherwise one should invent a
> sort of pickwickian assignment.

Yes, Dmitry, but it is this Pickwickian assignment that has been mooted, and
it is necessary for you to demonstrate that your proposal is superior.

To be precise, interpreting Randy's suggestion, my suggestion would be for
(Ada be changed in the next revision so that) an object declaration which
includes a subtype indication that denotes a class-wide type whose root type
is limited (tagged) to be permitted to include an (explicit) initialisation
expression, in which case special rules would apply to the dynamic semantics
for the elaboration of the declaration: (1) the subtype inidication is
elaborated; (2) the expression is evaluated and converted to the nominal
subtype; (3) the object is created (constrained according to the result from
(2)); (4) the value of the result of (2) is copied (bitwise) into the object
(no call to Initialize or Adjust will be made).

For example:


package Session_Management is

   type Internet_Session is abstract tagged limited private;

   function Open (Host: in String) return Internet_Session'Class;

   ...

private
   type Internet_Session is abstract new Ada.Finalization.Limited_Controlled
with null record;
end;

package body Session_Management is

   type Simple_Session (
         Host_Name_Length: Natural
         Traceback_Length: Natural) is
         new Internet_Session with
      record
         Host_Name: String(1..Host_Name_Length);
         Traceback: ?.Node_Array(1..Traceback_Length);
         Connection: ?.IP_Connection
         ...
      end record;

   function Open (Host: in String) return Internet_Session'Class;
   begin
      Open_Connection(Session.Connection,Host);
      declare
         Result: Simple_Session := (
            Host'Length,
            Get_Node_Count(Session.Connection),
            ... );
      begin
         ...
         return Result;
      end;
   exception
       ...
   end Open;

   ...

end Session_Management;

...

My_Sess: Internet_Session'Class := Open("ftp.cdrom.com");


The advantages of this idea, I think, are: it is a relatively simple change
to the Ada standard (although it is still not entirely simple); a function
such as 'Open' (call it a 'constructor function' if you wish, but it is
really a normal function) can be used to create temporary objects (that are
a part of a bigger expression) as well as for intialising objects; it seems
to me to be a natural and readily understood way of solving the problem (the
only twist being the change in the rules for a limited tagged type); (in
common with your propoal) all existing legal Ada source text will remain
totally unaffected. It seems to bear some similarility to the new rule for
aggregates.

My example above also demonstrates the use of an abstract type, which has
the advantage that the implementation (of the 'Open' function) can return
any type it likes (derived from Internet_Session). The user is happily
oblivious to which actual type is returned; the implementation can be
changed to return (further) different types according to new requirements
without disturbing the user (recompilation or relinking will be required,
but no changes to the user's source text). The type could be made concrete,
and more details about the type could then be revealed to the user (e.g. the
discriminants, the other record components), should that be considered more
appropriate.

> > I'd prefer making the function solution work. There is a proposal to
> > make it possible to initialize limited objects with a function call,
> > which would eliminate that objection.
>
> Yes it definitely would, because a function which result initialises an
> unconstructed object is just another name for constructor. However there
> would be also questions:
>
> 1. Will you use ":=" for limited types within a declaration with a problem
> to explain what is that, or just "rename"?

The use of ":=" would be my preference, since it does not require any change
to the syntax of object declarations (RM95 3.3.1).

> 2. What to do with "new" for limited types, when a constructor have to be
> called?

There was never any problem with allocator initialisation, because the
result is of an access type (which is always definite).

> 3. I suppose that constructors will be allowed for Limited_Controlled
only?
> So arrays, untagged types, tasks and protected object will remain out of
> reach.

My suggestion is that the new rules should only apply to object declarations
for limited class-wide (tagged) subtypes, because they are illegal now, so
we can be certain that existing legal Ada source text will not be affected
by the new rules.

It may be convenient, but it is not really necessary for protected or task
types to be included, because a tagged (record) type can always include a
component of the requisite protected or task type. The question does not
arise for array types, because they are not (able to be) limited (and so
always have assignment defined). Any definite type can be initialised by a
procedure to which it is passed as an 'in out' parameter. I think there may
be a case for the inclusion of indefinite limited untagged types.

Now, what am I missing? :-)

--
Nick Roberts






  reply	other threads:[~2002-12-29 18:45 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 [this message]
2002-12-30 12:23           ` Dmitry A. Kazakov
2002-12-30 15:14             ` Robert A Duff
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