comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
Subject: The "()" operator revisited.
Date: Mon, 12 Jan 2004 17:53:27 GMT
Date: 2004-01-12T17:53:27+00:00	[thread overview]
Message-ID: <raBMb.9128$Qq.7455@nwrdny01.gnilink.net> (raw)

Last June, I proposed that in order to provide an abstract array capability,
Ada 0y should support  user-defined "()" and "():="operators. Since this
issue has come up again, I would like to summarize and expand on this
proposal.

Note: the following discussion probably has an insufficient amount of
legalese to satisfy language lawyers. Once we've thrashed out these ideas,
I'll be happy to work with anyone with formalizing this proposal.

The idea is that container libraries, such as the Booch components, Charles
and PragMark, often include collection objects that can be viewed as
generalized arrays. Undoubtedly, the next standard will have some variation
of these container libraries. In support of these generalized arrays, Ada
should allow the array notation to be used for these object. To this end, I
propose adding two operators Ada. The "()" operator, used for reading a
component of an abstract array, has the form:

    function "()" ( Source  : in Abstract_Array_Type;
                    Index_1 : in Index_Type_1;
                    Index_2 : in Index_Type_2;
                    ...
                    Index_N : in Index_Type_N )
       return Component_Type;

Used to write a component of an abstract array, the "():=" operator has the
form

    procedure "():=" ( Source  : in out Abstract_Array_Type;
                       Index_1 : in Index_Type_1;
                       Index_2 : in Index_Type_2;
                       ...
                       Index_N : in Index_Type_N;
                       Value   : in Component_Type );

For a standard array type of the form

    type An_Array is array ( Index_Type_1 range <>, Index_Type_2 range <>,
... ) of Component_Type;

the "()" and "():=" operators are predefined. The predefined versions of
these operators are implicit. For packed arrays, the predefined versions of
"()" and "():="does the usual mask and shift operations to unpack or pack
the given component.

When the "()" function is defined and visible for the type Abstract_Array_Ty
pe, and A is a (possibly constant) object of type Abstract_Array_Type, then
the expression

    A( Index_1, Index_2, ... Index_N )

evaluates to

    "()"( A, Index_1, Index_2, ... Index_N )

Note that the "()" also provides support for an abstract function call
facility.

The "()" operator suffices for presenting a read-only view of a component of
an abstract array type. The "():=" is used in conjunction with "()" to
provide a variable view of an abstract array component. When "():=" operator
is defined and visible for Abstract_Array_Type, and A is a non-constant
object of type Abstract_Array_Type, then the expression

    A( Index_1, Index_2, ... Index_N )

can be assigned a Value of type Component_Type. This assignment is done by
performing the call

    "():="( A, Index_1, Index_2, ... Index_N, Value );

This "():=" call would also be used if A( Index_1, Index_2, ... Index_N ) is
used as an actual parameter for a parameter of mode "out"

When both "()" and "():=" operators are visible for Abstract_Array_Type,
then the expression

    A( Index_1, Index_2, ... Index_N )

can be used as an "in out" parameter. If we have the subprogram

    procedure Fiddle( Item : in out Component_Type );

Then the call

    Fiddle( A( I_1, I_2, ... I_N ) );

is basically equivalent to

    declare
       Temp_Comp : Component_Type := A( I_1, I_2, ... I_N );
    begin
       Fiddle( Temp_Comp );
       A( I_1, I_2, ... I_N ) := Temp_Comp;
    end;

except that the indices are evaluated only once. Note: something like this
is done in Ada95, when dealing with packed arrays.

We can easily define these operators for abstract arrays in Ada95. In the
string package Ada.Strings.Unbounded, we could add

    function "()" (Source : in Unbounded_String;
                   Index  : in Positive)
                   return Character
       renames Element;

    procedure "():=" (Source : in out Unbounded_String;
                      Index  : in     Positive;
                      By     : in     Character)
       renames Replace_Element;

Similar declarations can be added to Ada.Strings.Bounded. This would permit
the higher-level Ada string types to use the same notation as the low-level
standard String type.

Also, one can easily view a Direct I/O file as an abstract array. The
package Ada.Direct_Io can inline the following versions of "()" and "():="
for File_Type:

    function "()" (File  : in File_Type;
                   Index : in Positive_Count)
                   return     Element_Type
    is
       Result : Element_Type;
    begin
       Read( File, Result, From => Index );
       return Result
    end "()";

    procedure "():=" (File  : in File_Type;
                      Index : in Positive_Count;
                      Item  : in Element_Type)
    is
    begin
       Write( File, Item, To => Index );
    end "():=";

A controversial idea: when "()" and "():=" is visible for
Abstract_Array_Type, and A is a non-constant object of type
Abstract_Array_Type, should we allow a declaration like this:

    An_Object : Component_Type renames A( I_1, I_2, I_3, ... I_N );

The idea is that this is a "virtual" object of type Component_Type, where
calls to "()" and "():=" are used to read and write the values of this
object. I'm not entirely sure that this type of "renames" declaration is a
good idea, but I'm interested in what others in this NG think of this.





             reply	other threads:[~2004-01-12 17:53 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-12 17:53 Frank J. Lhota [this message]
2004-01-12 18:38 ` The "()" operator revisited Frank J. Lhota
2004-01-12 22:26 ` Robert A Duff
2004-01-13 16:29   ` Frank J. Lhota
2004-01-13  9:24 ` Dmitry A. Kazakov
2004-01-13 16:44   ` Frank J. Lhota
2004-01-13 17:13     ` Hyman Rosen
2004-01-13 22:27     ` Randy Brukardt
2004-01-14  2:30     ` Stephen Leake
2004-01-14  9:04     ` Dmitry A. Kazakov
2004-01-17  0:15       ` Kenneth Almquist
2004-01-17 21:15         ` Robert A Duff
2004-01-19 10:25         ` Dmitry A. Kazakov
2004-01-13 13:13 ` Marin David Condic
2004-01-13 17:38   ` Warren W. Gay VE3WWG
2004-01-13 19:09     ` Robert A Duff
2004-01-15 17:30       ` Warren W. Gay VE3WWG
2004-01-15 18:11         ` Robert A Duff
2004-01-15 19:36           ` tmoran
2004-01-15 20:35             ` Robert A Duff
2004-01-17  5:48               ` Robert I. Eachus
2004-01-16  1:52           ` Redefining := (was: The "()" operator revisited.) Jeffrey Carter
2004-01-16 21:37             ` Randy Brukardt
2004-01-19 11:33               ` Dmitry A. Kazakov
2004-01-16  3:11           ` The "()" operator revisited Mark A. Biggar
2004-01-16 13:28             ` Hyman Rosen
2004-01-16 16:19             ` Robert A Duff
2004-01-16 18:09             ` Warren W. Gay VE3WWG
2004-01-16 13:56           ` Frank J. Lhota
2004-01-16 16:14             ` Robert A Duff
2004-01-16 21:29               ` Frank J. Lhota
  -- strict thread matches above, loose matches on Subject: below --
2004-01-13 17:46 amado.alves
2004-01-13 22:21 ` Randy Brukardt
2004-01-13 17:53 amado.alves
2004-01-14  9:09 ` Dmitry A. Kazakov
2004-01-14 12:55   ` Georg Bauhaus
2004-01-14 15:05     ` Dmitry A. Kazakov
2004-01-15  1:21       ` Georg Bauhaus
2004-01-15  8:50         ` Dmitry A. Kazakov
2004-01-15 11:09           ` Georg Bauhaus
2004-01-15 13:23             ` Dmitry A. Kazakov
2004-01-17  6:26               ` Robert I. Eachus
2004-01-14 13:04   ` Hyman Rosen
2004-01-14 15:22 amado.alves
2004-01-14 16:16 ` 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