comp.lang.ada
 help / color / mirror / Atom feed
* Re: overloading predefined operators
       [not found] <1fabca7a-e3f0-41bb-9b51-9eabde85e800n@googlegroups.com>
@ 2022-06-24  8:23 ` Dmitry A. Kazakov
  2022-06-24  9:44   ` L. B.
  2022-06-25  3:18   ` Randy Brukardt
  2022-06-24 10:47 ` Jeffrey R.Carter
  1 sibling, 2 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2022-06-24  8:23 UTC (permalink / raw)


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;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: overloading predefined operators
  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
  1 sibling, 0 replies; 6+ messages in thread
From: L. B. @ 2022-06-24  9:44 UTC (permalink / raw)


Great. Thank you !!!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: overloading predefined operators
       [not found] <1fabca7a-e3f0-41bb-9b51-9eabde85e800n@googlegroups.com>
  2022-06-24  8:23 ` overloading predefined operators Dmitry A. Kazakov
@ 2022-06-24 10:47 ` Jeffrey R.Carter
  1 sibling, 0 replies; 6+ messages in thread
From: Jeffrey R.Carter @ 2022-06-24 10:47 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: overloading predefined operators
  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.
  1 sibling, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2022-06-25  3:18 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> 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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: overloading predefined operators
  2022-06-25  3:18   ` Randy Brukardt
@ 2022-06-25  5:58     ` L. B.
  2022-06-25  6:21       ` G.B.
  0 siblings, 1 reply; 6+ messages in thread
From: L. B. @ 2022-06-25  5:58 UTC (permalink / raw)


Randy,

> 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 

I tried this in
https://github.com/Blunk-electronic/ada_training/blob/master/src/redefining/equality_1/equality.adb
see line 99.

The compiler says:
equality.adb:99:20: incompatible arguments for operator



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: overloading predefined operators
  2022-06-25  5:58     ` L. B.
@ 2022-06-25  6:21       ` G.B.
  0 siblings, 0 replies; 6+ messages in thread
From: G.B. @ 2022-06-25  6:21 UTC (permalink / raw)


On 25.06.22 07:58, L. B. wrote:
> Randy,
> 
>> 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
> 
> I tried this in
> https://github.com/Blunk-electronic/ada_training/blob/master/src/redefining/equality_1/equality.adb
> see line 99.
> 
> The compiler says:
> equality.adb:99:20: incompatible arguments for operator

That's the compiler telling you that TYPE_FLOAT is not
type Float, the type of arguments needed for Standard."="
(see above).

See Jeffrey Carter's response for what to do to make them
that.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-06-25  6:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox