4 ms·
I got it working with: with Ada.Text_IO; use Ada.Text_IO; procedure Main is type Distance is new Float; type Area is new Float; function "*" (L
by bob-ROS 6y ago
I got it working with:
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Distance is new Float;
type Area is new Float;
function "*" (Left, Right : Distance) return Area is
temp : Distance;
begin
temp := Left * Right;
return Area (temp);
end "*";
D1 : Distance := 2.0;
D2 : Distance := 3.0;
A : Area;
begin
A := D1 * D2; -- OK
Put_Line(A'Image);
end Main;
Interestingly, it took me several attempts to create this overload, make it a single line return would make it recursive:
function "*" (Left, Right : Distance) return Area is
begin
return Left * Right;
end "*"; -- ^ Does not work
-- raised STORAGE_ERROR : stack overflow or erroneous memory access
Also:
function "*" (Left, Right : Distance) return Area is
begin
return Area (Left * Right);
end "*"; -- ^ Does not work either, compiler gives me "ambiguous operand in conversion"
- Asooka 6y agoCould you do return Area(Float (Left) * Float (Right));
- OneWingedShark 6y agoIIRC, the one-liner here [assuming you disabled the default "" operator] would be: Return Area( Distance'Base'(Left * Right) ); The apostrophe at the inner portion is 'qualification', a method for directing the compiler that the enclosed value is supposed to be a particular subtype. (Typically used to resolve ambiguities.)