comp.lang.ada
 help / color / mirror / Atom feed
From: Craig Carey <research@ijs.co.nz>
Subject: Re: Better control of assignment
Date: Tue, 13 Nov 2001 06:36:32 GMT
Date: 2001-11-13T06:36:32+00:00	[thread overview]
Message-ID: <kaf1vt8enmtk29s1b76vrkslh99qfioi00@4ax.com> (raw)
In-Reply-To: 3BEDA9C2.3FB191B7@acm.org

On Sat, 10 Nov 2001 22:27:25 GMT, Jeffrey Carter <jrcarter@acm.org>
wrote:

>Nick Roberts wrote:
>> 
>> "Jeffrey Carter" <jeffrey.carter@boeing.com> wrote in message
>> > Assignment should not be able to modify the RHS, especially as it may be
>> > an expression, not a variable.
>> 
>> Perhaps the name Assign is wrong. Maybe Copy would be better. To my mind, it
>> seems obvious (of course ;-) that if the RHS is a constant, this procedure
>> is not invoked; it is only invoked for a copy (including parameter passing
>> by copy (if selected)) from one variable object of type T to another. If
>> this does not deliver enough control, you must make the type private. For
>> example:
>> 
>>    type Odd_Counter is new Integer;
>>    procedure Assign_Odd (Src, Tgt: in out Odd_Counter);
>>    for Odd_Counter'Copy use Assign_Odd;
>>    ...
>>    procedure Assign_Odd (Src, Tgt: in out Odd_Counter) is
>>    begin
>>       if not Is_Odd(Src) then
>>          raise Oddness_Error;
>>       else
>>          Tgt := Src; -- inherited assignment used here by a language rule
>>       end if;
>>    end;
>
>There is still no need for Src to be mode in out.

---

>Nick Roberts wrote:
>> 
>> However, it seems feasible to me to introduce a new finalisation 
>> in the next revision. All we really need (am I wrong?) is three
>> attributes,
...
>>    procedure T'Construct (Object: in out T);
>>    procedure T'Destroy (Object: in out T);
>>    procedure T'Assign (Source, Target: in out T);
>
>Assignment should not be able to modify the RHS, especially as it may
> be an expression, not a variable.
>

Are you sure you want to allow Strings to be destroyed?.

Suppose the pointers got copied, as in this example:

procedure Crash_Ada is
   A, B, C : Unlimited_String;
begin
   Make_New (A);
   B.Ref := A.Ref;
   B.Length := ...
   C.Ref := A.Ref;
      --  At scope exit, the Destructor presumably is called onto all
      --  3 variables.
end Crash_Ada;

A fix is to stop the assignments to the ".Ref" fields.
There could be an agreement that we want to allow assigning to the
Length and Ref.all fields to quite available to packages with-ing
the package.

The whole program could crash when the Destructor frees the same
memory twice.

---

One problem is that letting the programmer do a swapping assign
instead of a copying assign, e.g.

       A :=. B

is that the routine holding B might have to be recoded like this:

From:

    procedure T (B : in Unlimited_String)

into:

    procedure T (B : in out Unlimited_String).

That is undesirable and it may be possible to allow records to have
read-only fields that can be altered (by routines declared in the same
package that declares the type), even though they are supposed to be
constant because they are an "in" mode parameter.

An aim here is to not force people to have pointers to pointers just
to avoid rewritng

   procedure F (B : in Unlimited_String.Vstr) is
   begin
      A :=. B;   --  Fast Assign with pointer swapping and loss of RHS
   end F;

as 

   procedure F (B : in Unlimited_String.Vstr) is
   begin
      A :=. B;
   end F;


It can be just a matter of adding the "out" mode to a record field
which makes the type have a read-only field.

An example of a new proposed feature for Ada:

package Q is
   type Vstr is
      record                             --  Ref is exported read only
         Ref      : out String_Access := Null_String_Access;
         Length   : Natural := 0;
      end record;
   procedure ":=." (L : out Vstr; R : in Vstr);
      --  Swaps pointers and changes the pointer value, R.Ref. It is
      --   read-only, not constant.
end Q;

package body Q is
   procedure ":=." (L : out Vstr; R : in Vstr) is
      Tmp    : Access_String;   --  Fast assign with ptr swapping
   begin
      LHS.Length := RHS.Length;
      Tmp := LHS.Ref;
      LHS.Ref := RHS.Ref;
      RHS.Ref := Tmp;
      RHS.Length := 0;     --  Invalidate the RHS pointer
   end ":=.";
end Y;

procedure R (B : in Q.Vstr) is
   A    : Q.Vstr;
begin
   Unknown1 (Alter_It => B);  --  This proc is not able to alter B.Ref
                              --  but it can change B.Ref.all.

   ...       --  Final access of the data from B destroys it.
             --  It is either that for inefficiency or lazy ":="-ing

   A :=. B;  --  B.Ref can be changed by the Fast Assignment operator
             --  since it is in the package defining (or extending)
             --   the type. That B (and B.Ref) is of the "in" mode
             --   would not stop any routine in Q from altering B.Ref.
   ...
end R;


A big advantage of that is that there are no pointers to pointers to
mislead the language into believing a value changed is not.


Maybe lazy assignments could be slower and it is not clear why such
a complex scheme ought be implented, with its destructors, when this
far simpler can't properly get destructors.

http://groups.google.com/groups?hl=en&rnum=11&selm=dewar.848839209%40merv

  > From: Robert Dewar
  > Subject: Re: Unbounded strings (Was: Java vs Ada 95 (Was Re: Once
  >   again, Ada absent from DoD SBIR solicitation)) 
  > Newsgroups: comp.lang.ada
  > Date: 1996/11/24 

Mr Dewar's lazy assignment idea might not be a remedy for the problem
of slow String handling code.

To put a pointer inside of a record and make it private or limited,
won't stop writing to the pointer while allowing writing to the ".all"
value. It is a bug in the language. No is "access constant [subtype]"
helpful.

Maybe M Carter can come up with an example arguing for user defining
of assignment that keeps away from Strings where what seem to be
language design defects become explicit.

When there are two pointers, then the first pointer can be made a
pointer to a 2nd pointer that is defined to be of this type:

   type [name] is access constant [second_pointer_subtype];

That stops all alteration of the B.Ref1.Ref2 while fully allowing
alterating of

     B.Ref1.Ref12.all (1 .. Sockets_Buffer_Maxlength).






  reply	other threads:[~2001-11-13  6:36 UTC|newest]

Thread overview: 162+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-02  3:56 List container strawman Ted Dennison
2001-11-02  4:20 ` James Rogers
2001-11-02 14:23   ` Ted Dennison
2001-11-02 14:38     ` Preben Randhol
2001-11-02 15:15     ` Larry Kilgallen
2001-11-02  4:35 ` Eric Merritt
2001-11-02 15:46   ` Ted Dennison
2001-11-02  7:28 ` Ehud Lamm
2001-11-02 14:57   ` Marin David Condic
2001-11-02 15:57     ` Francisco Javier Loma Daza
2001-11-02 16:28       ` Marin David Condic
2001-11-02 17:08         ` Ted Dennison
2001-11-02 15:06   ` Ted Dennison
2001-11-02 15:32     ` Marin David Condic
2001-11-02 16:33       ` Ted Dennison
2001-11-02 16:43         ` Marin David Condic
2001-11-02 22:51           ` Jeffrey Carter
2001-11-03  0:24             ` Matthew Heaney
2001-11-03  2:21               ` Jeffrey Carter
2001-11-03 22:51                 ` Rosen Trick [List container strawman] Nick Roberts
2001-11-04 13:07                   ` Robert Dewar
2001-11-04 17:17                     ` Side-Effects in Functions [Rosen Trick] Nick Roberts
2001-11-05  2:46                       ` Robert Dewar
2001-11-05  7:26                         ` pete
2001-11-05 10:29                           ` Dmitry A. Kazakov
2001-11-05 11:19                             ` pete
2001-11-05 14:59                               ` Dmitry A. Kazakov
2001-11-05 15:21                                 ` Preben Randhol
2001-11-05 16:04                                   ` Ted Dennison
2001-11-05 16:33                                   ` Dmitry A. Kazakov
2001-11-05 17:42                                     ` Warren W. Gay VE3WWG
2001-11-05 18:11                                       ` Preben Randhol
2001-11-06  8:38                                       ` Dmitry A. Kazakov
2001-11-06  9:31                                         ` tgingold
2001-11-06  0:10                             ` Nick Roberts
2001-11-06  9:30                               ` Dmitry A. Kazakov
2001-11-06 16:18                                 ` Lazy Evaluation [Side-Effects in Functions] Nick Roberts
2001-11-07  3:42                                   ` Robert Dewar
2001-11-07  4:42                                     ` Darren New
2001-11-07 11:54                                       ` Robert Dewar
2001-11-07 13:32                                         ` Florian Weimer
2001-11-07 13:24                                           ` Jean-Marc Bourguet
2001-11-09 18:06                                         ` Ted Dennison
2001-11-09 18:27                                           ` M. A. Alves
2001-11-11 20:13                                           ` Georg Bauhaus
2001-12-06 17:47                                             ` Harri J Haataja
2001-11-07  9:28                                   ` Dmitry A. Kazakov
2001-11-06 20:08                               ` Side-Effects in Functions [Rosen Trick] Florian Weimer
2001-11-06 22:48                                 ` Nick Roberts
2001-11-07 10:46                                   ` Florian Weimer
2001-11-05 13:56                           ` Robert Dewar
2001-11-05 16:08                             ` Ted Dennison
2001-11-05 17:44                               ` Warren W. Gay VE3WWG
2001-11-05 15:56                         ` Ted Dennison
2001-11-05 18:46                         ` Nick Roberts
2001-11-08 11:51                           ` Georg Bauhaus
2001-11-16  0:31                 ` List container strawman Vincent Marciante
2001-11-05 15:10             ` Marin David Condic
2001-11-05 18:31               ` Ted Dennison
2001-11-05 19:09                 ` Marin David Condic
2001-11-05 21:23                   ` Ted Dennison
2001-11-07 19:27                   ` Stephen Leake
2001-11-02 18:11         ` Mark Johnson
2001-11-02 18:46           ` Marin David Condic
2001-11-02 19:21           ` Larry Kilgallen
2001-11-03 22:30         ` Nick Roberts
2001-11-02 16:26   ` Ted Dennison
2001-11-02 16:36     ` Marin David Condic
2001-11-02 19:31       ` Ted Dennison
2001-11-02 17:49     ` Jeffrey Carter
2001-11-08 10:34     ` Ehud Lamm
2001-11-08 18:53       ` Better Finalisation [List container strawman] Nick Roberts
2001-11-09 13:36         ` Robert Dewar
2001-11-09 15:04           ` Florian Weimer
2001-11-10  0:36           ` Nick Roberts
2001-11-09 15:16         ` Ted Dennison
2001-11-09 17:30         ` Better control of assignment Jeffrey Carter
2001-11-10  0:32           ` Nick Roberts
2001-11-10 22:27             ` Jeffrey Carter
2001-11-13  6:36               ` Craig Carey [this message]
2001-11-13  6:39               ` Craig Carey
2001-11-13  8:53               ` Craig Carey
2001-11-14  9:42                 ` Craig Carey
2001-11-09 14:49       ` List container strawman Ted Dennison
2001-11-09 16:12         ` Ehud Lamm
2001-11-09 17:12         ` Marin David Condic
2001-11-09 18:11           ` Ted Dennison
2001-11-09 18:42           ` Matthew Heaney
2001-11-10 17:54             ` Simon Wright
2001-11-02 14:49 ` Marin David Condic
2001-11-02 15:15   ` Ted Dennison
2001-11-02 15:37     ` Marin David Condic
2001-11-02 16:49       ` Ted Dennison
2001-11-02 17:09         ` Marin David Condic
2001-11-04  0:10           ` Nick Roberts
2001-11-03 23:41         ` Nick Roberts
2001-11-02 17:02 ` David Botton
2001-11-02 17:55   ` David Botton
2001-11-03 19:22 ` Nick Roberts
     [not found] ` <3BE29AF4.80804@telepath.com>
2001-11-02 13:14   ` Ted Dennison
2001-11-02 13:31     ` Larry Kilgallen
2001-11-02 15:09       ` Ted Dennison
2001-11-02 15:13         ` Preben Randhol
2001-11-02 20:48       ` David Starner
2001-11-02 22:49         ` Larry Kilgallen
2001-11-02 17:44     ` Jeffrey Carter
2001-11-02 20:07       ` Ted Dennison
2001-11-02 23:19         ` Jeffrey Carter
2001-11-03  6:56           ` Ted Dennison
2001-11-03 19:22             ` Jeffrey Carter
2001-11-04 18:58               ` Darren New
2001-11-04 19:40                 ` Larry Kilgallen
2001-11-04 20:49                   ` Darren New
2001-11-07 19:07                   ` ramatthews
2001-11-08  0:04                     ` Darren New
2001-11-08  4:50                     ` Jeffrey Carter
2001-11-08 23:26                       ` ramatthews
2001-11-09 18:00                     ` Ted Dennison
2001-11-09 18:13                       ` Jean-Marc Bourguet
2001-11-09 18:55                         ` Ted Dennison
2001-11-10  1:48                           ` Nick Roberts
2001-11-10 17:04                             ` Ted Dennison
2001-11-10 20:59                               ` Nick Roberts
2001-11-10 23:17                                 ` Larry Hazel
2001-11-11  3:27                                   ` Nick Roberts
2001-11-12 18:39                                     ` Darren New
2001-11-13  0:35                                       ` Nick Roberts
2001-11-10 19:36                             ` Ehud Lamm
2001-11-10 20:15                               ` Nick Roberts
2001-11-09 19:27                       ` Larry Kilgallen
2001-11-09 20:03                       ` Stephen Leake
2001-11-09 21:05                         ` Ted Dennison
2001-11-09 22:42                         ` Larry Kilgallen
2001-11-10  4:52                           ` Nick Roberts
2001-11-10 20:24                       ` ramatthews
2001-11-05 19:28                 ` Ted Dennison
2001-11-05 19:42                   ` Jean-Marc Bourguet
2001-11-05 20:40                     ` Ted Dennison
2001-11-05 20:24                   ` Darren New
2001-11-05 20:45                     ` Ted Dennison
2001-11-05 17:21         ` List container strawman; Construct alternatives Stephen Leake
2001-11-03  7:42       ` List container strawman Simon Wright
2001-11-05 14:00   ` Stephen Leake
2001-11-08 11:17     ` Simon Wright
2001-11-13 16:29       ` Stephen Leake
2001-11-13 22:43         ` Jeffrey Carter
2001-11-13 22:48         ` Jeffrey Carter
2001-11-14  3:46           ` Nick Roberts
2001-11-15 10:23             ` Ehud Lamm
2001-11-14 14:50           ` Marin David Condic
2001-11-14 16:53             ` Jeffrey Carter
2001-11-14 17:59               ` Marin David Condic
2001-11-15  3:33                 ` Nick Roberts
2001-11-15 15:10                   ` Marin David Condic
2001-11-16  1:29                     ` Nick Roberts
2001-11-16 16:03                       ` Marin David Condic
2001-11-16 20:19                         ` Nick Roberts
2001-11-15 18:08                   ` Matthew Heaney
2001-11-08 14:57 ` M. A. Alves
2001-11-09  2:00   ` Jeffrey Carter
2001-11-09 18:31   ` Ted Dennison
2001-11-10 19:56     ` Ehud Lamm
replies disabled

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