comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Proposal: Constructors, Assignment [LONG]
Date: Wed, 01 Jan 2003 15:13:44 +0100
Date: 2003-01-01T15:13:44+01:00	[thread overview]
Message-ID: <auut21$aa0ff$1@ID-77047.news.dfncis.de> (raw)
In-Reply-To: v14dhge1o45s61@corp.supernews.com

Randy Brukardt wrote:

> Dmitry A. Kazakov wrote in message ...
>>I do not understand how could this work. According to Nick Roberts the
>>result of Make_Session is copied as-is. But the object returned by
>>Make_Session has to be pointed from OUTSIDE:
> 
> Right. That's the "new feature" in the proposal. And it's relatively
> cheap, since compilers typically handle composite functions by creating
> memory at the point of the call and passing it in to the function with a
> hidden parameter. Might as well make that explicit.
> 
>>function Make_Session (Count: Integer) return Internet_Session is
>>   Result : Internet_Session (Count);
>>begin
>>--
>>-- Place the new object in front of the list
>>--
>>   Result.Previous := Global_List_Header'Unchecked_Access;
>>   Result.Next := Global_List_Header.Next;
>>   Global_List_Header.Next := Result'Unchecked_Access;
>>   Global_List_Header.Next.Previous := Result'Unchecked_Access;
>>   return Result;
>>end Make_Session;
>>
>>How to fix this?
> 
> The syntax of the proposal wasn't determined, but the basic idea was
> something like:
> 
> function Make_Session (Count: Integer) return Internet_Session is
>    Result : return Internet_Session (Count);
> begin
> --
> -- Place the new object in front of the list
> --
>    Result.Previous := Global_List_Header'Unchecked_Access;
>    Result.Next := Global_List_Header.Next;
>    Global_List_Header.Next := Result'Unchecked_Access;
>    Global_List_Header.Next.Previous := Result'Unchecked_Access;
>    return Result;
> end Make_Session;
> 
> With this declaration, "Result" is passed in implicitly from the caller
> for a limited type, and it must be returned. Returning some other object
> or none at all is an error. There can be only one such object (only one
> object can be passed in).

I see, the old FORTRAN's idea to expose the result as a variable! I am very 
impressed, that such a thing managed to come through. (:-))

I suppose it will be allowed for all types? Then it would be interesting to 
know how it will work with by-value types:

function Set_Bit (Value : Boolean) return Boolean is
   Result : return Boollean := Value;
begin
   return Result;
end Set_Bit;

... provided that the result is an element of a packed array. Somehow, 
somewhere the "in-place" requirement have to be relaxed. I wonder how many 
pages one will add to RM.

Then there will be much work required to deal with things like:

function Make_Session (Count: Integer) return Internet_Session is
begin
   declare
      Result1 : return Internet_Session (24);
   begin
      if ... then
         return Result1;
      end if;
   end;
   declare
      Result2 : return Internet_Session (1000); - another size
   begin
      if ... then
         return Result2;
      end if;
   end;
   declare
      Result3 : Internet_Session (10); -- I forgot "return"
   begin
      return Result3;
   end;   
end Make_Session;

I am not sure, but to determine whether there is only one result, could be 
equivalent to halting problem. So some sort of run-time support will be 
well required to determine this. Or will it be classified as one more bound 
error?

>>...
>>Absolutely. The rest is easy, it is just to explain why "not-copying"
> have
>>to be spelled as copying:
>>
>>X : Obj := Func (<args>);
>>
>>Isn't it misleading? We already have functions that are not functions.
> Now
>>we will have even more non-functions, and also non-results of
>>non-assignments. I wonder why all this is not counted as a "new
> feature"?
> 
> Because we already have it in Ada 95 for aggregates (see AI-83 and the
> corrigendum). So we're just extending what's already available.
> 
>>...
>>Class-wide objects cannot be constructed.
> 
> I don't get this at all. What's wrong with:
> 
>     A : T'Class := Construct_Class (...);
> 
> where the definition of Construct_Class uses some mechanism to dispatch
> properly.
> 
> (Note that you virtually always end up with a case statement in
> Construct_Class, because you somehow have to get from a set of arbitrary
> parameter values to some tag. There is no automatic way to do that in
> any programming language that I know of. If you're really clever, you
> can use T'Class'Input to do this, but its a real hack.)

Yes, this is why my proposal offers a way to write things working like 
T'Class'Input. When T'Class is created it (1) calls a user-defined function 
to determine Tag, then (2) it dispatches to a user-defined function to get 
constraints and finally (3) it dispatches to a user-defined Initialize.

As I understood from your and other's responses the new call syntax "Func 
(...) use Tag" would give 1. 2-3 would be covered by new "FORTRAN" 
functions and an ability to "assign" what cannot be assigned. So there 
indeed will be a way to write Construct_Class without a case statement:

function Construct_Class (...) return T'Class is
begin
   -- figure out the tag
   declare
      Result : return T'Class := Construct_Object (...) use <tag>;
   begin
      retrun Result;
   end;
end Construct_Class;

function Construct_Object (...) return T is
begin
   -- figure out the discriminants
   declare
      Result : return T := T (<discriminants>);
   begin
      -- initialize other Result's fields
      retrun Result;
   end;
end Construct_Object;

Formally it should work. (?)

-- 
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



  reply	other threads:[~2003-01-01 14:13 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
2002-12-31 13:02               ` Dmitry A. Kazakov
2003-01-01  0:28                 ` Randy Brukardt
2003-01-01 14:13                   ` Dmitry A. Kazakov [this message]
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