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.4 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FORGED_MUA_MOZILLA,FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,5a88548f1bcf3510 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.66.90.8 with SMTP id bs8mr253014pab.24.1352992386310; Thu, 15 Nov 2012 07:13:06 -0800 (PST) Path: s9ni13279pbb.0!nntp.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 15 Nov 2012 09:13:05 -0600 Date: Thu, 15 Nov 2012 10:13:05 -0500 From: "Peter C. Chapin" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121028 Thunderbird/16.0.2 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Overring function and its returned type References: <97a6946f-a707-4dd3-872b-9e851fcf9462@googlegroups.com> In-Reply-To: <97a6946f-a707-4dd3-872b-9e851fcf9462@googlegroups.com> Message-ID: X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-ObQhUaZbL2crz61rpgbPShDz3qWR9UuLht39qI/o1aHmKkuQo2rUBirNGmDwBryzzc+pv6SEsR6Cr0H!ee0j1Z0hoy9n0eRD/2PYFhaaJEyFgGGhPkTmOh4jwYE+Vx/kVsDk5Ioe5BoE9NA= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3315 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Date: 2012-11-15T10:13:05-05:00 List-Id: On 11/09/2012 02:57 PM, sbelmont700@gmail.com wrote: > More generally, you are talking about 'covariant return types', which > is a feature only a handful of languages support (notably C++). > IMHO, i don't really see the need for it; a type is either a child > (and matches the parent, as in P3) or it doesn't and it's not (as in > P2). I realize this thread is dead but I found it interesting and looked into the topic a little. It is true that C++ supports covariant refinement of return types as does, for example, Scala. In theory it should also be type safe to allow contravariant refinement of parameter types. Scala does not allow that... which surprised me considering Scala's reputation of having a very expressive type system. I talked with some people in the Scala community and it turns out the reason for the restriction is related to overload resolution and ambiguity. In the case of C++ there is no overloading on return types and thus allowing covariant refinement of return types is straight forward there. However, since Ada does allow overloading on return types, complications arise. To illustrate let me start with a simple derivation class: package Vehicles is type Vehicle is interface; type Car is abstract new Vehicle with null record; type Sports_Car is abstract new Car with null record; end Vehicles; Now consider: with Vehicles; package Animals is type Animal is interface; function F(A : Animal) return Vehicles.Vehicle'Class is abstract; function F(A : Animal) return Vehicles.Car'Class is abstract; type Cat is abstract new Animal with null record; overriding function F(C: Cat) return Vehicles.Sports_Car'Class is abstract; end Animals; Suppose covariant refinement of return types was allowed in Ada. In that case which F is being overridden here? Since Sports_Car <: Vehicle and also Sports_Car <: Car the overriding is ambiguous. I suppose rules could be written to clarify these things but it probably wouldn't be worth the additional language complexity. Peter