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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham 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!postnews.google.com!s13g2000cwa.googlegroups.com!not-for-mail From: "Adam Beneschan" Newsgroups: comp.lang.ada Subject: Re: Subtype conformance... not what I was expecting. Date: 26 Jul 2006 14:23:27 -0700 Organization: http://groups.google.com Message-ID: <1153949007.159580.193020@s13g2000cwa.googlegroups.com> References: <44c6db66$0$2928$4d3efbfe@news.sover.net> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1153949013 30756 127.0.0.1 (26 Jul 2006 21:23:33 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 26 Jul 2006 21:23:33 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: s13g2000cwa.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news2.google.com comp.lang.ada:5952 Date: 2006-07-26T14:23:27-07:00 List-Id: 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. I don't know what the language designers' rationale was for requiring the subtypes to statically match, although I do think that trying to define a one-way "subtype conformance" that requires subtypes for IN parameters and function results to be "statically a subset" of another type may just be too complex to be worth the effort if it's not needed by real programs. However, I do foresee a possible problem if the rules were loosened. In your example, if a function has a parameter of subtype "Narrow", a compiler could work things out so that the parameter could be passed in an 8- or 16-bit register, or something like that. This probably wouldn't make a difference in a simple example like yours. But on some processors, and if there were a larger number of parameters, this sort of optimization *could* make a difference. But a compiler wouldn't be able to do this if there were a possibility that, in some other library package (say), there were an access-to-subprogram type that were compatible with *both* this function *and* a similar function whose parameter has type "Integer" (assuming Integer is 32 bits). Under the current language rules, I don't think this is a problem. -- Adam