Version 1.1 of acs/ac-00147.txt

Unformatted version of acs/ac-00147.txt version 1.1
Other versions for file acs/ac-00147.txt

!standard 4.9.1(1.2/2)          07-10-15 AC95-00147/01
!class confirmation 07-10-15
!status received no action 07-10-15
!status received 07-06-15
!subject Limited types, initialization, and type conversion
!summary
!appendix

From: Stephen W Baird
Sent: Tuesday, July 31, 2007  5:06 PM

The following example is illegal, but it seems that what the user is
trying to accomplish is reasonable:

  procedure Test is
    package Pkg is
        type T is tagged limited null record;
        function F (X : T) return T;
    end Pkg;
    use Pkg;

    type Ref is access all T'Class;

    function Alloc (Ptr : Ref) return Ref is
    begin
        return new T'Class'(T'Class (F (Ptr.all)));
    end Alloc;

    package body Pkg is separate;
  begin
    null;
  end Test;

This example is illegal because 7.5(2.1/2) doesn't allow type conversions.

In private correspondence, Pascal pointed out that this restriction
could be worked around by adding a function:

    function Alloc (Ptr : Ref) return Ref is
      function Foo return T'Class is
      begin
         return F (Ptr.all);
      end Foo;
    begin
       return new T'Class'(Foo);
    end Alloc;

This function allows the conversion from T to T'Class to be performed
implicitly rather than explicitly.

Thus, we have a situation where we disallow a clearer, more concise expression
of a program but allow a dynamically equivalent version which requires an
auxiliary function whose purpose does not seem obvious.

Pascal also observed that this was similar to the situation with AI05-0032:
    "It seems that there are analogies: in both cases
     class-wide types are involved, and in both cases an extra function
     is needed to fix the problem."
and suggested that this discussion may belong in that AI.

There are good reasons for 7.5(2.1/2)'s treatment of type conversions
in the case where the target type of the conversion is specific.

On the other hand, allowing a conversion to a class-wide type which includes
the type of its operand does not introduce any new cases that
cannot already be produced using Pascal's conversion function trick.

I think we should consider modifying 7.5(2.1/2) to allow such type
conversions.

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

From: Tucker Taft
Sent: Tuesday, July 31, 2007  5:26 PM

I'm a little unclear why in the example you don't
just write:

     return new T'(F(Ptr.all));

What are the contexts where you need to qualify
with a class-wide type?

In any case, I don't have an objection to allowing
conversions to a class-wide type, though I am
curious in what "copying" contexts they would be needed.

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

From: Stephen W Baird
Sent: Wednesday, August 1, 2007  3:52 PM

> I'm a little unclear why in the example you don't just write:
>
>      return new T'(F(Ptr.all));

Good point.

> In any case, I don't have an objection to allowing
> conversions to a class-wide type, though I am
> curious in what "copying" contexts they would be needed.

It sounds like this is "insufficiently broken".

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

[Editor's note: October 15, 2007]

It is clear that only a few kinds of type conversions would be safe
in build-in-place contexts. Surely, we couldn't allow anything that
could change representation or discard portions of a type. That
suggests that allowing those kinds of conversions could be confusing
for the user, as the rules would appear rather arbitrary. Thus, I don't
think this is a good idea.

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


Questions? Ask the ACAA Technical Agent