by tmcb · 5y ago · 0 comments
Nothing very high-level, as we need to explicitly manipulate the single-component records, but it does the job.
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Distance is record
Value : Float;
end record;
type Area is record
Value : Float;
end record;
function "*" (Left, Right : Distance) return Area is
begin
return (Value => Left.Value * Right.Value);
end "*";
D1 : constant Distance := (Value => 10.0);
D2 : constant Distance := (Value => 20.0);
-- D3 : constant Distance := D1 * D2;
-- Does not compile. GNAT returns the following error:
-- main.adb:21:33: expected type "Distance" defined at line 4
-- main.adb:21:33: found type "Area" defined at line 8
A : constant Area := D1 * D2;
begin
Put_Line ("Area A is " & Float'Image(A.Value));
end Main;*as in:
with Ada.Text_IO; use Ada.Text_IO;
with System.Dim.Float_Mks; use System.Dim.Float_Mks;
with System.Dim.Float_Mks_IO; use System.Dim.Float_Mks_IO;
procedure Main is
subtype Distance is System.Dim.Float_Mks.Length;
subtype Area is System.Dim.Float_Mks.Area;
D1 : constant Distance := 10.0*m;
D2 : constant Distance := 20.0*m;
-- D3 : constant Distance := D1 * D2;
-- Does not compile. GNAT returns the following error:
-- main.adb:13:08: dimensions mismatch in object declaration
-- main.adb:13:37: expected dimension [L], found [L**2]
A : constant Area := D1 * D2;
begin
-- print: Area A is 2.00000E+02 m**2
Put ("Area A is ");
Put (Item => A);
Put_Line ("");
end Main; Function "*"(Left, Right: Distance) return Distance is abstract;
then use: Function "*"(Left, Right : Distance) return Area;