comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Subtype conformance... not what I was expecting.
Date: Wed, 26 Jul 2006 09:54:48 +0200
Date: 2006-07-26T09:53:52+02:00	[thread overview]
Message-ID: <mo3rtwrhvvqv$.10l5wotai1a7h$.dlg@40tude.net> (raw)
In-Reply-To: 44c6db66$0$2928$4d3efbfe@news.sover.net

On Tue, 25 Jul 2006 23:02:56 -0400, Peter C. Chapin wrote:

> I'm experimenting with Ada's handling of access to subprogram types. I 
> was surprised to discover that the following example does not work (I'm 
> using GNAT GPL 2006):
> 
> 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
>     begin
>        return Para;
>     end F;
> 
>     G      : Function_Ptr := F'Access;
>     Result : Integer;
> begin
>     Result := G(0);
> end Check;
> 
> The compiler complains about the initialization of G with F'Access 
> saying that it is not "subtype conformant." However, I believe this 
> initialization would be type safe. Since F's argument types are super 
> types of G's argument types, there is no context where G can be called 
> that would violate the constraints on the underlying arguments of F. 
> Similarly since F's return type is a subtype of G's return type, 
> anything F might return would be acceptable as a return from G. I 
> assumed that this was what subtype conformance was about, but apparently 
> not.
> 
> In fact, GNAT appears to require the argument and return subtypes to 
> match exactly. However, this seems overly restrictive. I'm curious about 
> the rationale for this restriction.

The semantic of "subtype" in Ada is "same type." So if you allow Narrow to
appear in place of Integer, you must also allow the reverse:

    subtype Narrow is Integer range -10..10;
    type Function_Ptr is access function(X : Narrow) return Narrow;
    function F(Para : Integer) return Integer;
        -- Constraint_Error-unsafe

If you wanted a one-way road, you'd need function(X : Narrow) return
Integer be an override of some primitive subprogram of Integer. That would
make you able to legally judge about conformance to *class* (Narrow <:
Integer). But that works for only operations defined on the class.
Unfortunately Ada does not have either Integer'Class or Narrow'Class.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2006-07-26  7:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-26  3:02 Subtype conformance... not what I was expecting Peter C. Chapin
2006-07-26  6:58 ` Craig Carey
2006-07-26  7:54 ` Dmitry A. Kazakov [this message]
2006-07-26 14:32   ` Peter C. Chapin
2006-07-26 15:38     ` Georg Bauhaus
2006-07-27  0:02       ` Peter C. Chapin
2006-07-26 18:40     ` Dmitry A. Kazakov
2006-07-27  0:00       ` Peter C. Chapin
2006-07-26  8:06 ` Georg Bauhaus
2006-07-26 14:15   ` Peter C. Chapin
2006-07-26 21:23 ` Adam Beneschan
replies disabled

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