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,FREEMAIL_FROM autolearn=ham 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,UTF8 Received: by 10.180.104.38 with SMTP id gb6mr1057452wib.4.1352507340343; Fri, 09 Nov 2012 16:29:00 -0800 (PST) X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.116.97 Path: q13ni211144wii.0!nntp.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!nntpfeed.proxad.net!dedibox.gegeweb.org!gegeweb.eu!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: =?utf-8?Q?Yannick_Duch=C3=AAne_=28Hibou57?= =?utf-8?Q?=29?= Newsgroups: comp.lang.ada Subject: Re: Overring function and its returned type Date: Sat, 10 Nov 2012 01:28:54 +0100 Organization: Ada @ Home Message-ID: References: <97a6946f-a707-4dd3-872b-9e851fcf9462@googlegroups.com> NNTP-Posting-Host: aWaWeUaBdaj2Zzc04J1v5A.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.02 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: Quoted-Printable Date: 2012-11-10T01:28:54+01:00 List-Id: Le Fri, 09 Nov 2012 22:10:05 +0100, Yannick Duch=C3=AAne (Hibou57) = a =C3=A9crit: > I will also try to workaround with an overloading instead of an = > overriding, but I'm afraid of getting into a mess with this solution. = = > Will choose which cause the less troubles, and although the = > postcondition+conversion solution looks like bloat, I believe that's = > still the safest. I will still try both to really get an idea=E2=80=A6= Two attempts, with a comment comparing both at the end. First attempt =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Something I tried, which lead to an interesting issue. I don't know if = GNAT is right or wrong, this will require a careful reading of the RM. Replace `P3` spec with: package P3 is type R is new P1.R with null record; type T is new P1.T with null record; overriding function F (E : T) return P1.R'Class with Post'Class =3D> F'Result in R'Class; -- Not a primitive operation. function F (E : T'Class) return R'Class is (R'Class (P1.T (E).F)); end; And a use case, which fails: A : P3.T; B : P1.R'Class :=3D A.F; C : P3.R'Class :=3D A.F; -- Fails here That's C's assignment which fails. Seems GNAT cannot resolve it figure i= t = refers to the second `F` function (it does not even complains about an = ambiguity). Will have to look at the RM for it. I feel this should be OK= = and could be resolved as expected, but may be the RM states otherwise. The second `F` function cannot be a primitive, due to its =E2=80=9Cinlin= e=E2=80=9D = expression. Second attempt =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Another way to solve it: package P3 is type R is new P1.R with null record; type T is new P1.T with null record; overriding function F (E : T) return P1.R'Class with Post'Class =3D> F'Result in R'Class; not overriding function F (E : T) return R'Class; end; -- ---------------------------------------- package body P3 is overriding function F (E : T) return P1.R'Class is X : R; begin return X; end; not overriding function F (E : T) return R'Class is begin return R'Class (P1.T (E).F); end; end; And the sample use case: A : P3.T; B : P1.R'Class :=3D A.F; -- Fails here C : P3.R'Class :=3D A.F; Assignment to `B` is considered ambiguous, but that's not an issue, as = only the case corresponding to the assignment to `C` is expected. Anyway= , = the case of the assignment to `B` can be solved this way: B : P1.R'Class :=3D P1.R'Class'(A.F); -- Resolved ambiguity Comparing both =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D What's nice with the first attempt, is that what's returned is explicitl= y = shown in the spec. What's nice with the second attempt, is that it works= = (good point, cheese), and that it's a primitive (if ever that matters = really), but as a cons, it is not as explicit as in the first attempt. May be the best solution is to do as in the second attempt, and give the= = second `F` function, a comment =E2=80=9Cspecifying=E2=80=9D the returned= result is exactly = the same as the with the first `F`, expect explicitly of type `R'Class`.= That's not as clean as one could expect, but at least I don't see it as = = presenting any potential issues in the case where `P1` holds a primitive= = with a predicate referring to `F`. The human reader could infer such a = predicate would applies to the second `F` function too, as long as it is= = clear enough both returns the same thing, except for their explicit type= s. = The compiler won't know, but the human reader will. -- = =E2=80=9CSyntactic sugar causes cancer of the semi-colons.=E2=80=9D [1] =E2=80=9CStructured Programming supports the law of the excluded muddle.= =E2=80=9D [1] [1]: Epigrams on Programming =E2=80=94 Alan J. =E2=80=94 P. Yale Univers= ity