From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,T_SCC_BODY_TEXT_LINE, XPRIO autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: overloading predefined operators Date: Fri, 24 Jun 2022 22:18:37 -0500 Organization: A noiseless patient Spider Message-ID: References: <1fabca7a-e3f0-41bb-9b51-9eabde85e800n@googlegroups.com> Injection-Date: Sat, 25 Jun 2022 03:18:39 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="ea8ee915547ce677c25b93936a1e0f4c"; logging-data="16329"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bbRaxNSYKXr4sUPil1jaWxFkT7ujhb1Y=" Cancel-Lock: sha1:xFWes+eGJxnc844y0uPiev/Np58= X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.7246 X-RFC2646: Format=Flowed; Response X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-Priority: 3 X-MSMail-Priority: Normal Xref: reader02.eternal-september.org comp.lang.ada:64030 List-Id: "Dmitry A. Kazakov" wrote in message news:t93sak$10r4$1@gioia.aioe.org... > On 2022-06-24 10:10, L. B. wrote: >> I have a question regarding redefining of operators like "=". My dummy >> code is: >> >> type my_float is new float; >> >> -- This overloads the predefined equality operation: >> function "=" (left, right : in my_float) return boolean is begin >> .... >> end "=" >> >> a, b : my_float; >> >> a := 0.01 >> b := 0.009; >> >> -- This test uses the overloading equality test: >> if a = b then -- uses the new "=" >> null; >> end if; >> >> -- For some reasons I still need access to the overloaded original "=" >> function: >> if a = b then -- shall use the original "=" >> null; >> end if; >> >> What can I do ? > > Rename the inherited operation before killing it: > > type M_Float is new Float; > function Equal (Left, Right : M_Float) return Boolean renames "="; > function "=" (Left, Right : M_Float) return Boolean; This was important enough to the Ada 95 team that they gave it a name -- "a squirreling rename" (as in "squirreling away"). You'll find that in the index of the AARM to this day. There's actually some special rules which allow this rename in cases where without those rules you wouldn't be able to do this. It's important in some cases I generally prefer to use prefix notation in such cases rather than renaming (which is easy to get subtly wrong). Remember that you can call any Ada operator as if it is a normal function. So: if Standard."=" (A, B) then gives you the original "=" (which is defined in Standard for Standard.Float). Randy.