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 ?
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 ?
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;
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
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
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 379 |
Nodes: | 16 (2 / 14) |
Uptime: | 44:39:43 |
Calls: | 8,141 |
Calls today: | 4 |
Files: | 13,085 |
Messages: | 5,858,055 |