Re: Subtype conformance... not what I was expecting.



On Tue, 2006-07-25 at 23:02 -0400, Peter C. Chapin wrote:

procedure Check is
subtype Narrow is Integer range -10..10;
type Function_Ptr is access function(X : Narrow) return Integer;

function F(Para : Integer) return Narrow is
...
G : Function_Ptr := F'Access;

The compiler complains about the initialization of G with F'Access
saying that it is not "subtype conformant." ...
However, this seems overly restrictive.

Would you still think the same in the following variation? (I don't know
the rationale for the restriction but then I wasn't surprised :-)

procedure Check is
subtype Above_Zero is Integer range 1 .. 95; -- from liquid to for tea
subtype Below_Zero is Integer range -100 .. -5; -- really solid

type Frozen_Ptr is access function(X: Below_Zero) return Integer;
type Liquid_Ptr is access function(X: Above_Zero) return Integer;

function Whirl(Para: Integer) return Above_Zero is
begin
return Para;
end;

function Cut(Para: Integer) return Below_Zero is
begin
return Para;
end;

G: Frozen_Ptr := Whirl'access;
Ice: Below_Zero;
begin
Ice := G(0);
end Check;


Above_Zero and Below_Zero let me think of different logical types, really.
The set of values in the respective subtypes is disjoint.

I won't want to deliver hot water to/from a function that needs/returns
cold ice. In my view, this will be a contract violation, in this case
at least.

Maybe the restriction helps avoid specimens of "I'm the programmer, and
I know that the program is right, even though it's not obvious".


-- Georg


.