comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R.Carter" <spam.jrcarter.not@spam.acm.org.not>
Subject: Re: overloading predefined operators
Date: Fri, 24 Jun 2022 12:47:10 +0200	[thread overview]
Message-ID: <t944ne$us7$1@dont-email.me> (raw)
In-Reply-To: <1fabca7a-e3f0-41bb-9b51-9eabde85e800n@googlegroups.com>

On 2022-06-24 10:10, L. B. wrote:
> 
> type my_float is new float;
> 
> function "=" (left, right : in my_float) return boolean is begin
>   ....
> end "="
> 
> a, b : my_float;
> 
> a := 0.01
> b := 0.009;
> 
> -- 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 ?

With a derived type like this, you can convert back to the parent type:

if Float (A) = Float (B) then

With a completely new type, you can only name one of the operations "=". You 
have to decide which one you want to call "=", and which one you want to call 
something else, like Equal. So, for

type Real is digits 7;

you can either call your new operation Equal

function Equal (Left : in Real; Right : in Real) return Boolean;

which leave "=" for the predefined operation, or you can rename the predefined 
operation to Equal as Kazakov has described

function Equal (Left : in Real; Right : in Real) return Boolean renames "=";
function "=" (Left : in Real; Right : in Real) return Boolean;

-- 
Jeff Carter
"[I]t is foolish to polish a program beyond the
point of diminishing returns, but most programmers
do too little revision; they are satisfied too
early."
Elements of Programming Style
189

      parent reply	other threads:[~2022-06-24 10:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1fabca7a-e3f0-41bb-9b51-9eabde85e800n@googlegroups.com>
2022-06-24  8:23 ` overloading predefined operators Dmitry A. Kazakov
2022-06-24  9:44   ` L. B.
2022-06-25  3:18   ` Randy Brukardt
2022-06-25  5:58     ` L. B.
2022-06-25  6:21       ` G.B.
2022-06-24 10:47 ` Jeffrey R.Carter [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox