From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,628d2a493f1e203d X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!news.glorb.com!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!xindi.nntp.kabelfoon.nl!newsfeed.freenet.de!news.osn.de!diablo2.news.osn.de!noris.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Subtype conformance... not what I was expecting. Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <44c6db66$0$2928$4d3efbfe@news.sover.net> Date: Wed, 26 Jul 2006 09:54:48 +0200 Message-ID: NNTP-Posting-Date: 26 Jul 2006 09:53:52 MEST NNTP-Posting-Host: 59162a79.newsread4.arcor-online.net X-Trace: DXC=;9cfNm>03j2d9772DFAUn2:ejgIfPPld4jW\KbG]kaM8ea\9g\;7Nm5SW3;h_FolC5[6LHn;2LCV>7enW;^6ZC`4IXm65S@:3>? X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:5927 Date: 2006-07-26T09:53:52+02:00 List-Id: 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