comp.lang.ada
 help / color / mirror / Atom feed
* The "()" operator revisited.
@ 2004-01-12 17:53 Frank J. Lhota
  2004-01-12 18:38 ` Frank J. Lhota
                   ` (3 more replies)
  0 siblings, 4 replies; 45+ messages in thread
From: Frank J. Lhota @ 2004-01-12 17:53 UTC (permalink / 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.





^ permalink raw reply	[flat|nested] 45+ messages in thread
* RE: The "()" operator revisited.
@ 2004-01-13 17:46 amado.alves
  2004-01-13 22:21 ` Randy Brukardt
  0 siblings, 1 reply; 45+ messages in thread
From: amado.alves @ 2004-01-13 17:46 UTC (permalink / raw)
  To: comp.lang.ada

<<...To get something that could match a generic array parameter would require a
lot of restrictions on abstract arrays, in particular that the indice types
would have to be abstract discrete types. This would rule out abstract
arrays such as what we see in this example.>>

Which would be unfortunate. One solution would to add abstract arrays to the set of generic parameters. This whole proposal should articulate with other proposals: user-defined ":=", implicit conversion. Also, there was already a thread on the current issue, some months ago. Implicit conversion could solve the problems of having non-discrete values for the index, and literals. All together this would be a really big change to the language and there is no way it's going to make it in Ada 2005. As someone already said, better write the language from scratch. It is a fascinating issue, but academic.

The alternative is to have abstract arrays as a severely restricted class (perhaps a subclass of arrays). Actually the original proposal so it seems. But I doubt it makes in time to Ada 2005.

In sum, I must have too much free time on my hands to be posting this, because I don't believe any currently appearing proposal is going to make it in time for Ada 2005, and I fear Ada 2005 is going to be the last Ada. With luck we'll be designing a new language, incorporating the lessons learnt from Ada 1983-2005.



^ permalink raw reply	[flat|nested] 45+ messages in thread
* RE: The "()" operator revisited.
@ 2004-01-13 17:53 amado.alves
  2004-01-14  9:09 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 45+ messages in thread
From: amado.alves @ 2004-01-13 17:53 UTC (permalink / raw)
  To: Hyman Rosen, comp.lang.ada

"You should begin to see why C++ templates don't come with a mechanism
to specify restrictions on parameters, and instead rely on seeing if
usage within the code is legal."

Right, and on Future_Language we will have the best of both worlds: any degree of restriction on the parameter definition. Currently I simulate classes outside the established set with formal packages.



^ permalink raw reply	[flat|nested] 45+ messages in thread
* RE: The "()" operator revisited.
@ 2004-01-14 15:22 amado.alves
  2004-01-14 16:16 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 45+ messages in thread
From: amado.alves @ 2004-01-14 15:22 UTC (permalink / raw)
  To: comp.lang.ada

"Templates are for generic programming...."
 
Generics is the poor man's dynamic program construction. Ada generics are in the tradition of strong compile-time type checking. I dream of unifying strong checking with dynamic construction. Intentional programming perhaps. Maybe that can be done playing with ASIS. Anyway types as objects/values would be required I think.
 



^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2004-01-19 11:33 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-12 17:53 The "()" operator revisited Frank J. Lhota
2004-01-12 18:38 ` 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

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