Version 1.8 of ais/ai-00252.txt

Unformatted version of ais/ai-00252.txt version 1.8
Other versions for file ais/ai-00252.txt

!standard 04.01.03 (09)          03-10-23 AI95-00252/06
!standard 04.01.03 (15)
!standard 06.03.01 (10)
!class amendment 00-12-04
!status Amendment 200Y 03-10-23
!status ARG Approved 9-0-1 03-10-04
!status work item 00-12-04
!status received 00-12-04
!priority High
!difficulty Hard
!subject Object.Operation notation
!summary
An equivalence is proposed between Object.Operation(...) and Package.Operation(Object,...) to allow an object-oriented programming model that is based on applying operations to objects, rather than selecting operations from a package and then applying them to parameters.
!problem
In Ada 95, one must identify the particular package in which an operation is defined, in addition to the primary "controlling" object to which the operation is to be applied. Identifying both the package and the object is to some extent redundant, and makes object-oriented programming in Ada 95 wordier than necessary, and/or encourages heavy use of potentially confusing "use" clauses. Eliminating this redundancy would make object-oriented programming less-error prone, while also clarifying what object is the controlling object.
!proposal
We propose a syntactic shorthand Object.Operation(...) as an equivalent to Package.Operation(Object, ...) where "Package" is the package in which Object's type (or covering thereof) is declared. The equivalence would be based on a "use"-like visibility model, where after the ".", primitive and classwide operations from the package in which an object's type is declared, as well as classwide operations from packages in which types that "cover" the object's type are declared, would be effectively "use" visible. Further, the first parameter to the operations made "use" visible would (implicitly) be the object preceding the ".". This syntactic shorthand would be provided for objects of a tagged type, or objects designating a tagged type. NOTE: We considered generalizing this to allow non-tagged types to use this shorthand, but this become complex when the type is an access type, since both the access type itself, and its designated type must be considered. Furthermore, the benefit is lower since there is no issue of classwide operations for non-tagged types, so all the "interesting" operations are all from a single package.
The "use"-like visibility model ensures that the operations made visible would be hidden by any components with the same identifier as the operation (since they are "directly" visible after the ".", rather than "use" visible). In addition, no "beaujolais" effects would be produced, because the operations made visible would all overload one another, rather than having operations from one package hide another.
The visible components are considered prior to the (implicitly) use-visible declarations. As usual, the "normally" visible components can hide the use-visible declarations, but the use-visible declarations cannot hide each other (they only overload each other).
Because of access parameters, if the type of the prefix is an access-to-tagged type, the package(s) in which the designated type and classwide types that cover the designated type would be "use"d. Furthermore, to preserve the model that "." can result in an implicit dereference, an interpretation of the prefix as an implicit dereference is coupled with a "use" of the packages in which the designated type and its coverings are declared. In this implicit dereference case, the package where the access type itself is declared is irrelevant.
As a final addition, if the object is aliased, interpretations where 'Access would be needed after the object name are considered. That is, even if the prefix is not of an access type, operations with the first parameter being an access parameter designating the type of the prefix are considered. This would mean that access parameters can be used in general in primitives, without penalizing the user who actually declares an (aliased) object on the stack rather than using the heap.
One way of describing what is happening is that operations declared in the package immediately enclosing a tagged type declaration that have the type (or "access" type or type'Class) as their first parameter, are implicitly declared as components of a type, but with the first parameter removed. These implicitly declared components are then inherited when the type is extended. Use-visibility rules are used for these implicitly-declared components to avoid "beaujolais" effects.
Note that if the prefix type is neither tagged nor access to tagged, no shorthand is defined. If the type is tagged, it makes available the classwide operations from the packages where "coverings" are declared, plus the primitives from the package where the type itself is declared. If the type is an access-to-tagged type, it makes available the operations that would have been made available for the designated type.
!wording
Add the following after paragraph 4.1.3(9):
* A view of a subprogram whose first formal parameter is of a tagged type or is an access parameter designating a tagged type.
The prefix (after any implicit dereference) shall resolve to denote an object or value of a specific tagged type T or class-wide type T'Class. The selector_name shall resolve to denote a view of a subprogram declared immediately within the region in which an ancestor of the type T is declared. The first formal parameter of the subprogram shall be of type T, or a class-wide type that covers T, or an access parameter designating one of these types. The designator of the subprogram shall not be the same as that of a component of the tagged type visible at the point of the selected_component. The selected_component denotes a view of this subprogram that omits the first formal paramete.
Add the following after para 4.1.3(15) of dynamic semantics:
For a selected_component with a tagged prefix and selector that denotes a view of a subprogram, a call on the view denoted by the selected_component is equivalent to a call on the underlying subprogram with the first actual parameter being provided by the object or value denoted by the prefix (or the Access attribute of this object or value if the first formal is an access parameter), and the remaining actual parameters given by the actual_parameter_part, if any.
Add the following after 6.3.1(10):
* the view of a subprogram denoted by a selected_component whose prefix denotes an object or value of a tagged type, and whose selector_name denotes a subprogram operating on the type (see 4.1.3).
!discussion
This AI grew out of an issue identified by Erhard Ploedereder and his graduate students, where it made OOP awkward to always identify (or "use") the specific package in which a dispatching operation was declared, particularly when the operation was inherited, and hence only implicitly declared in that package. Furthermore, the rules for calling classwide operations and primitive operations were significantly different, where the classwide operation was not inherited, and hence "remains" in the original package where it was declared, whereas the primitive operations were inherited, and hence got carried along into the package where the type was declared. The tendency was to "use" every package that might conceivably have an operation of interest, which can significantly add to the confusion.
We considered an Object'Operation(...) syntax, but that was felt to introduce possible conflicts with implementation-dependent attributes. Also, the "." notation had the additional nice feature that a primitive function could be used to effectively provide a "read only" component, with the familiar "." syntax. Also, using the "." notation allows primitives defined outside a protected or a task type to be called in the same "obj.operation" notation used for entries and protected subprograms. This allows tagged primitive operations to be called with the same style, potentially making programs more consistent.
We considered only making primitive operations visible, but there are situations where an abstraction uses a classwide operation very much like a primitive operation. For example in Claw, whether a given operation is classwide or primitive is not particularly relevant to how it is used (though of course it is relevant to how it is handled in type extensions). The "package"-oriented approach, as opposed to the "primitive"-oriented approach, may also fit more cleanly into existing overload resolution algorithms, since it has more in common with the way "use" visibility works currently. Of course, any guess of implementation burden is hard to make, since compilers have so many different strategies.
When using this notation, class-wide operations can be used which are declared in any package which declares an ancestor of the type of the object. We considered only allowing class-wide operations declared in the same package as the specific type of the object. However, this would mean that a different set of operations would be visible for the calls Op (Obj, ...) and Obj.Op(...) when all of the ancestor packages have use clauses (a common situation). That means that changing from one form of the call to the other could potentially change the operation called.
Adding the implicit .all and "'Access" seem like useful capabilities, without significantly complicating the proposal.
We talk about "covering" types rather than "ancestor" types for two reasons. One is that it is only operations on class-wide types that are being imported from packages other than the one in which the type itself is declared. Second, the notion of "covers" will presumably generalize better if we adopt the notion of abstract interfaces (see the multiple inheritance Amendment AI).
Note that operations on class-wide types can be used even if the package in which they are declared is not "with"ed. This is part of the whole point. A class-wide operation may be introduced anywhere in the type hierarchy, and it will be available via the object.operation(...) notation without having to identify the specific package. Since only packages where the "coverings" of the type are declared are considered, this "implicit with" is not actually creating any new semantic dependences. Essentially it is as though these class-wide operations are being inherited, but with no possibility of overriding. They are like "final" operations in Java.
We originally generalized this to support non-tagged types, but the added complexity this brought to handling access types seemed more than the anticipated benefit, since we would have to consider primitives of the access type itself as well as those of its designated type.
!example
Here is an example of use of the "object.operation" syntax:
package P is type T is tagged ... -- implicit declaration of T'Class procedure Prim(X : in out T); procedure Classwide(X : in out T'Class; Y : Integer); end P;
with P; package P2 is type T2 is new P.T with ... -- implicit declaration of T2'Class -- implicit declaration of Prim(X : in out T2); procedure Prim2(X : in out T2; B : Boolean); function Prim3(X : T2) return Float; end P2;
with P2; procedure Main is Obj : P2.T2; CObj : P2.T2'Class := ... begin Obj.Prim; -- call on inherited primitive Obj.Prim2(True); -- call on primitive CObj.Prim; -- dispatching call Obj.Classwide(Y => 77); -- call on classwide op if CObj.Prim3 > 33.5 then -- dispatching call on primitive function ... end if; end Main;
Here is an example using a prefix that is of an access type.
with P2; package P3 is type T3 is new P2.T2 with ... procedure Prim4(A : access T3; C : Character); -- a primitive of T3 using an access param end P3;
with P3; package P4 is type AT3 is access all T3'Class; end P4;
with P4; procedure AccMain is Ptr : P4.AT3 := new ...; begin Ptr.Prim; -- Implicit dereference, equivalent to Ptr.all.Prim Ptr.Prim4(C => 'x'); -- No implicit dereference; "P3" package "use"ed -- because desig type declared there
end AccMain;
!corrigendum 4.1.3(9)
Insert after the paragraph:
the new paragraph:
!corrigendum 4.1.3(15)
Insert after the paragraph:
For a selected_component that denotes a component of a variant, a check is made that the values of the discriminants are such that the value or object denoted by the prefix has this component. The exception Constraint_Error is raised if this check fails.
the new paragraph:
For a selected_component with a tagged prefix and selector that denotes a view of a subprogram, a call on the view denoted by the selected_component is equivalent to a call on the underlying subprogram with the first actual parameter being provided by the object or value denoted by the prefix (or the Access attribute of this object or value if the first formal is an access parameter), and the remaining actual parameters given by the actual_parameter_part, if any.
!corrigendum 6.3.1(10)
Replace the paragraph:
by:
!ACATS test
Create ACATS tests for this notation.
!appendix

From: Tucker Taft
Sent: Sunday, November 26, 2000 10:49 AM
Subject: Object.Operation amendment AI

Here is an amendment AI that might be considered part of
the series of AIs designed to "round out" the OOP features.
This one was prompted in part by the reactions of Erhard's
grad students to the difficulty of having to both identify
the package containing an operation and the object on which
the operation is to be performed.  With both classwide
and primitive operations being relevant, and these operations
having essentially opposite rules about which package the
operation resides in (the ultimate ancestor for classwide,
and the ultimate descendant for primitive), some way to
eliminate the package from the syntax seemed useful.

The other prompting factor is the continual whining that Ada 95
is out of the mainstream of OOP languages because it lacks the
object.operation syntax.  This proposal defines the "object.op"
syntax as essentially a syntactic sugar on the pkg.op(object,...)
syntax.  This approach is pretty much what Modula-3 did.  It
provides for a "symmetric" notation when dealing with binary
operators, while also providing an "asymmetric" (object-oriented)
syntax when using operations that have a single controlling
operand.

As usual, any and all comments highly encouraged.
-Tuck
-------------
!standard 04.01.03 (05)                               00-11-25  AI95-xxx/01
!class amendment 00-11-25
!priority High
!difficulty Hard
!subject Tagged Types, Object.Operation Notation, Object-Oriented Programming

!summary

An equivalence is proposed between Object.Operation(...) and
Package.Operation(Object,...) to allow an object-oriented programming model
that is based on applying operations to objects, rather than selecting
operations from a package and then applying them to parameters.

!question

[Note: I am interpreting this "question" section as a statement of the problem
that might deserve an amendment.]

In Ada 95, one must identify the particular package in which an
operation is
defined, in addition to the primary "controlling" object to which the operation
is to be applied. Identifying both the package and the object is to some extent
redundant, and makes object-oriented programming in Ada 95 wordier than
necessary, and/or encourages heavy use of potentially confusing "use" clauses.
Would it be possible to eliminate this redundancy, while also clarifying what
object is the controlling object.

!recommendation

We propose an equivalence between Object.Operation(...) and
Package.Operation(Object, ...) where "Package" is the package in which Object's
type (or covering thereof) is declared. The equivalence would be based on a
"use"-like visibility model, where after the ".", operations from the package in
which an object's type is declared, as well as packages in which types that
"cover" the object's type are declared, would be effectively "use" visible.
Further, the first parameter to the operations made "use" visible would
(implicitly) be the object preceding the ".".

The "use"-like visibility model ensures that the operations made visible would be
hidden by any components or protected operations with the same identifier as the
operation (since they are "directly" visible after the ".", rather than "use"
visible). In addition, no "beaujolais" effects would be produced, because the
operations made visible would all overload one another, rather than having
operations from one package hide another.

Because of access parameters, if the type of the prefix is an access type, the
package(s) in which the designated type and types that cover the designated type
would be "use"d, in addition to the package in which the access type itself is
declared. Furthermore, to preserve the model that "." can result in an implicit
dereference, an interpretation of the prefix as an implicit dereference is
coupled with a "use" of the packages in which the designated type and its
coverings are declared. In this implicit dereference case, the package where the
access type itself is declared is irrelevant.

!example

Here is an example of use of the "object.operation" syntax:

package P is
    type T is tagged ...
    -- implicit declaration of T'Class
    procedure Prim(X : in out T);
    procedure Classwide(X : in out T'Class; Y : Integer);
end P;

with P;
package P2 is
    type T2 is new P.T with ...
    -- implicit declaration of T2'Class
    -- implicit declaration of Prim(X : in out T2);
    procedure Prim2(X : in out T2; B : Boolean);
    function Prim3(X : T2) return Float;
end P2;

with P2;
procedure Main is
    Obj : P2.T2;
    CObj : P2.T2'Class := ...
begin
    Obj.Prim;         -- call on inherited primitive
    Obj.Prim2(True);  -- call on primitive
    CObj.Prim;        -- dispatching call
    Obj.Classwide(Y => 77);  -- call on classwide op
    if CObj.Prim3 > 33.5 then  -- dispatching call on primitive function
        ...
    end if;
end Main;

Here is an example using a prefix that is of an access type.

with P2;
package P3 is
    type T3 is new P2.T2 with ...
    procedure Prim4(A : access T3; C : Character);
      -- a primitive of T3 using an access param
end P3;

with P3;
package P4 is
    type AT3 is access all T3'Class;
    procedure APrim5(Q : AT3; R : Integer);
      -- a primitive of AT3 (not of T3)
end P4;

with P4;
procedure AccMain is
    Ptr : P4.AT4 := new ...;
begin
    Ptr.Prim;             -- Implicit dereference, equivalent to Ptr.all.Prim
    Ptr.Prim4(C => 'x');  -- No implicit dereference; "P3" package
"use"ed
                          --  because desig type declared there
    Ptr.Aprim5(R => 13);  -- No implicit dereference; "P4" package
"use"ed
                          --  because AT4 declared there
    if Ptr."="(null) then -- "=" declared in P4 so can be called this
way also
        ...
    end if;
end AccMain;


!discussion

This AI grew out of an issue identified by Erhard Ploedereder and his graduate
students, where it made OOP awkward to always identify (or "use") the specific
package in which a dispatching operation was declared, particularly when the
operation was inherited, and hence only implicitly declared in that package.
Furthermore, the rules for calling classwide operations and primitive operations
were significantly different, where the classwide operation was not inherited,
and hence "remains" in the original package where it was declared, whereas
the primitive operations were inherited, and hence got carried along into
the package where the type was declared.  The tendency was to "use" every
package that might conceivably have an operation of interest, which can
significantly add to the confusion.

We considered an Object'Operation(...) syntax, but that was felt to introduce
possible conflicts with implementation-dependent attributes.  Also, the "."
notation had the additional nice feature that a primitive function could be used
to  effectively provide a "read only" component, with the familiar "." syntax.
Also, using the "." notation allows primitives defined outside a protected or a
task type to be called in the same "obj.operation" notation used for entries and
protected subprograms.  This unifies these two kinds of operations, which from a
user perspective are both "fundamental" operations of the synchronizing types.

We considered only making primitive operations visible, but there are situations
where an abstraction uses a classwide operation very much like a primitive
operation. For example in Claw, whether a given operation is classwide or
primitive is not particularly relevant to how it is used (though of course it is
relevant to how it is handled in type extensions).  The "package"-oriented
approach, as opposed to the "primitive"-oriented approach, may also fit more
cleanly into existing overload resolution algorithms, since it has more in common
with the way "use" visibility works currently.  Of course, any guess of
implementation burden is hard to make, since compilers have so many different
strategies.

The rules for access types are a bit convoluted, but they seem necessary given
the importance of access parameters.

We talk about "covering" types rather than "ancestor" types for two reasons.
One is that it is only operations on class-wide types that are being imported from
packages other than the one in which the type itself is declared.  Second,
the notion of "covers" will presumably generalize better if we adopt the notion
of abstract interfaces (see the multiple inheritance Amendment AI).

****************************************************************

From: Alexander Kopilovitch
Sent: Monday, June  9, 2003 10:09 AM

!summary

An alternative syntax is proposed here for AI-252. This alternative uses
new attribute instead of extension for meaning of dot-qualification, which is
currently proposed in AI-252. This text assumes the context of current AI-252.

!proposal

Let us introduce new attribute Method, which always must be followed by dot
and operation, i.e.

  Object'Method.Operation

for example:

  type T is ... ;
  function F(A1 : T; A2 : Integer) return Integer;
  procedure P(A1: T);
  ...

  X : T;
  Actual_A2 : Integer;
  F_Result : Integer;
  ...

  F_Result := X'Method.F(Actual_A2);
  ...
  X'Method.P;

Conceptually, the attribute Method returns a record -- the table of all
relevant methods; some analogy with C++ code is present here, although the
major difference is obvious: contents of this table depends not only on the
object's type, but on surround and visibility rules also.

Basic visibility rules for Operation may stay as they are stated currently
in AI-252, but with additional option: a programmer can explicitly list all
visible packages for a particular subtype using appropriate "for...use"
statement:

  for Subtype'Method use Package1 [, Package2, ..., PackageN];

Such explicit statement overrides basic visibility rules for all
Object'Method.Operation expressions where Object belongs to Subtype.

Further, with this approach we may easily provide a denotation for the
component-on-demand abstraction, that is, unified notation for an externally
immutable view of a data component and a function; all we need for this is
another attribute Property, which should be used the same way:

  Object'Property.Function_Or_DataField

for example, for both

  type T is ... ;
  function F(P : T) return Integer;

and

  type T is record
     F : Integer;
     ...
  end record;

we can write:

  X : T;
  R : Integer;
  ...

  R := X'Property.F;

Arguments for a function prefixed by the Property attribute naturally
correspond indices for array data component.

Further, arrays (that are indexed by controlling object type) likewise may be
used in conjunction with the Property attribute  (in accordance with analogy
between arrays and functions, supported in Ada). For example:

  type String_Index is new Integer;
  S : String(1..10);
  I : String_Index;
  ...
  ... I'Property.S ...

Even multi-dimensional arrays are permitted here. For example:

  type Cities is (Edinburgh, Glasgow, London, Paris, New_York);
  type Distances is array (Cities, Cities) of Float;
  Km_To : Distances := ...;
  ...
  ... Edinburgh'Property.Km_To(Paris) ...

As for arrays of arrays, only outer array may be used, that is, the subscripts
for inner arrays cannot appear. For example:

  type Table_Index is new Integer;
  type Table_Line is String(1..50);
  Table : array (Table_Index range 1..10) of Table_Line;
  I : Table_Index;
  ...
  ... I'Property.Table ...     -- legal
  ...
  ... I'Property.Table(1) ...  -- illegal


!discussion

Object.Operation syntax seems as acceptable compromise in a case when
there is a controlling object. Although even then the Operation does not
belong to the Object (as it belongs to a package), it is reasonable to claim
that conceptually, the status of being controlling temporary gives the Object
some additional rights over all its operations, and in particular, extends
visibility rules for the Object.

But in many cases there are no controlling objects, and in those cases this
Object.Operation syntax will act against proper expression and understanding
of program design and logic. Moverover, as this Object.Operation style
potentially conflicts with package-orientation, which is fundamental feature
of Ada 95, and as this Object.Operation notation is compulsory in most of
today's mainstream languages, there is real possibility of massive and
disordered mixture of those design styles if this Object.Operation notation
appear in Ada.

There is also anxiety about possible confusion with component notation.
As Robert I. Eachus recently wrote in comp.lang.ada newsgroup
(message-id <3ED056CB.8000200@attbi.com> ):
"I really don't like the idea of allowing the Object.Operation format to
Ada.  Yeah, the compilers can usually figure it out understand it. But
the potential confusion with component notation is going to confuse
experienced Ada users.  (And eventually other O-O language users when
the actually run into component references."

An alternative proposed here attains main purpose of AI-252, and it does not
contest AI's propositions concerning all things except Object.Operation
syntax. At the same time it establishes a deterrent for unjustified use of
the feature (by extra wording), makes the expression of programmer's intent
more explicit, and additionally, provides finer control over visibility and
over interchangeability between operation and data field.

The level of uniformity achieved with the notation proposed here is even
higher than with dot-notation proposed in current AI-252 (because the arrays
are included) without sacrificing traditional features and natural ways for
expressing specific intentions.

****************************************************************

From: Craig Carey
Sent: Wednesday, June 11, 2003  3:47 AM

The AI-00252 maybe does not go far enough: it is not about putting ","s
after types.

Here is an example that would occur when creating Ada bindings to C++
*.h files. Two unsatisfactory details are that "Cpp.A.A" & "Cpp.A.Value"
are to be used instead of "A.A" and "A.Value".

package Cpp is

   package A is
      type A_Rec is tagged limited null record;
      subtype A is A_Rec'Class;
      function Value return A;
   end A;

   package B is
      subtype A is Cpp.A.A;
      procedure F (T : A := A.Value);  -- Not legal, the
                        --  A in "A.Value" is a subtype
   end B;

end Cpp;

If the "subtype A is Cpp.A.A;" is replaced with "use A;" then
the error shifts to the "A" in the "A :=", with the problem now
being that it can't see the type but thinks that that A is a
package.

If both "subtype A is Cpp.A.A;" and "use A;" are used, then the
error returns back to the "A.Value" with the ObjectAda compiler
saying that the "A" in "A.Value" is the type "A" and not the
package "A".

The language could be improved to better resolve overlaps of
subtype names and package names. (Maybe AI-00252 could consider
this (it might eventually consider dots after tokens naming
subtypes)).


At 2003\06\09 19:09 +0400 Monday, Alexandre E. Kopilovitch wrote:
>!summary
>
...

That proposal suggested two keywords ("Method", and "Property").
That is general and there I doubt arguments for that would show
up. AI-00252 said 0 special tokens and a "'" is best.

****************************************************************

From: Craig Carey
Sent: Wednesday, June 11, 2003  8:07 AM

Ada could have a 'class' feature. It can be implemented in that way,
for example:

------------------------------------------------------------
package Cpp is
   type Y is tagged limited null record;

   package A is      -- The "<>" means that this is a class
      type A_Rec is new Y with   -- (or "is tagged")
         record
            N : Integer;
         end record;

      subtype <> is A_Rec'Class;         --  Glue this subtype into whole package
      type A_Ptr is access constant A;   --  this "A" is the "<>" type

      Static  : aliased A_Rec;
      function Value return A;
   end A;

   Var   : A.A_Ptr := A.Static'Access;

   package B is
      procedure F (T : A := A.Value);  -- Legal now
   end B;

end Cpp;
------------------------------------------------------------

In general, statements like this:

   subtype X is X.Y;

just spill out the contents of record Y into the larger surrounding
X record.
The word "renames" is an alternative to "subtype".

By this means, abstract multiple inheritance could be implemented without
a use of the tagged type. Instead of dispatching when procedures are
called, the programmer explicitly specifies the subrecord by gluing on
extra text after a dot.

****************************************************************

From: Tucker Taft
Sent: Saturday, September 27, 2003  4:11 PM

Here is an update to AI 252.  Not much change (Randy
already incorporated some of the changes from Toulouse).
I added a rule to disallow object.op if "op" is the name
of a visible component of "object."  I also added
a paragraph to 6.3.1 to include object.op as an example
of a subprogram with an intrinsic convention.
Finally, I changed "object" to "object or value" to
be consistent with other paragraphs in 4.1.3, and to cover the
weird cases where the prefix is not officially an
"object" (e.g. my_array_type(tagged_array)(2) is a
"value" rather than an "object" -- this distinction is
pretty silly at this point).

[This is version /05 - ED.]

****************************************************************

Questions? Ask the ACAA Technical Agent