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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,72c34c66b38e0e05 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-12-29 10:45:28 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!uni-berlin.de!pc-62-30-113-45-cr.blueyonder.co.UK!not-for-mail From: "Nick Roberts" Newsgroups: comp.lang.ada Subject: Re: Proposal: Constructors, Assignment [LONG] Date: Sun, 29 Dec 2002 18:45:21 -0000 Message-ID: References: NNTP-Posting-Host: pc-62-30-113-45-cr.blueyonder.co.uk (62.30.113.45) X-Trace: fu-berlin.de 1041187525 8852571 62.30.113.45 (16 [25716]) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-Mimeole: Produced By Microsoft MimeOLE V5.50.4522.1200 Xref: archiver1.google.com comp.lang.ada:32380 Date: 2002-12-29T18:45:21+00:00 List-Id: "Dmitry A. Kazakov" 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