我正在尝试创建一个具有默认值的子程序参数的 Ada 通用包。我无法让编译器识别默认值。我猜这是由于可见性。有没有办法在泛型声明中转发声明函数?
通用规格:
generic
type Item is private;
type Item_Ref is access all Item;
Addr : System.Address;
Default : Item;
with Is_Valid (Obj : Item) return Boolean;
-- Forward Declare ** DOES NOT COMPILE
function Default_Validate (Data_Ptr : Item_Ref) return Boolean;
with function Validate (Data_Ptr : Item_Ref) return Boolean is Default_Validate;
package Foo is
-- function Default_Validate (Data_Ptr : Item_Ref) return Boolean;
function Read_Eeprom return Item;
end Foo;
通用机构:
package body Foo is
Obj : aliased Item;
for Obj'Address use Addr;
-- Read Method
function Read_Eeprom return Item is
begin
-- ** Read EEPROM using OBJ **
Validate (Obj'Unchecked_Access);
end Read_Eeprom;
-- Default Validate Method
function Default_Validate (Data_Ptr : Item_Ref) return Boolean is
Valid : Boolean;
begin
Valid := Is_Valid(Data_Ptr.all);
if not Valid then
Data_Ptr.all := Default;
end if;
return Valid;
end Default_Validate;
end Foo;
司机
with Foo;
procedure Main is
MAX_INT : constant Integer := 100;
MIN_INT : constant Integer := 0;
-- Special / Non-Scaler Type
type Pair_Type is
record
X : Integer;
Y : Integer;
end record;
type Pair_Ref is access all Pair;
-- Is Valid
function Int_Is_Valid(Int : Integer) return Boolean is
begin
return (Int <= MAX_INT and Int >= MIN_INT);
end Pair_Is_Valid;
-- Is Valid
function Pair_Is_Valid(Pair : Pair_Type) return Boolean is
begin
return Pair.X'Valid and Pair.Y'Valid;
end Pair_Is_Valid;
-- Validate
function Pair_Validate(Pair : Pair_Ref) return Boolean is
Valid : Boolean := True;
begin
if not Pair.X'Valid then
Pair.X := 0;
Valid := False;
end if;
if not Pair.Y'Valid then
Pair.Y := 0;
Valid := False;
end if;
return Valid;
end Special_Validate;
type Int_Ref is access all Integer;
My_Int : Integer;
My_Pair : Pair_Type;
Default_Pair : Pair_Type := (0,0);
package Int_Obj is new Foo (Item => Integer,
Item_Ref => Int_Ref,
Addr => My_Int'Address,
Default => 0,
Is_Valid => Int_Is_Valid);
package Pair_Obj is new Foo (Item => Pair_Type,
Item_Ref => Pair_Ref,
Addr => My_Pair'Address,
Default => Default_Pair,
Is_Valid => Pair_Is_Valid,
Validate => Pair_Validate);
Tmp_Int : Integer;
Tmps_Pair : Pair_Type;
begin
Tmp_Int := Int_Obj.Read_Eeprom;
Tmp_Pair := Pair_Obj.Read_Eeprom;
end Main;
我得到的错误是“预期文件结尾,文件只能有一个编译单元”如何将通用子程序默认为作为包成员的函数?