Version 1.5 of ais/ai-00252.txt

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

!standard 04.01.03 (05)          03-06-22 AI95-00252/04
!class amendment 00-12-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 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 of a specific tagged type T or class-wide type T'Class. The selector_name shall resolve to denote 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 selected_component denotes a view of this subprogram that omits the first formal parameter, and has convention Intrinsic.
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 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 denoted by the prefix (or the Access attribute of this object if the first formal is an access parameter), and the remaining actual parameters given by the actual_parameter_part, if any.
!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.
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;
!ACATS test
!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).

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

Questions? Ask the ACAA Technical Agent