Ada 2012为类型提供了Default_Value
方面。是否有任何方法可以限制泛型类型参数以要求它们具有默认值(或者是否有检查任何方面的通用方法?)
基本上,我的问题是下面的例子是否安全。当前gnat抛出警告
main.adb:6:03:警告:变量"Var"已读取但从未分配[-gnawv
当我没有用with Default_Value => 10.0
定义MyFloat时。
-- main.adb
with MyProc;
procedure test is
type MyFloat is new Float with Default_Value => 10.0;
package P is new MyProc (MyFloat);
Var : P.Bla;
Var2 : MyFloat := P.Stuff (Var);
begin
null;
end test;
-- MyProc.ads
generic
type MyTypeWithDefault is private;
package MyProc is
type Bla is tagged private;
function Stuff (Self : Bla) return MyTypeWithDefault;
private
type Bla is tagged record
Data : MyTypeWithDefault;
end record;
end MyProc;
-- MyProc.adb
package body MyProc is
function Stuff (Self : Bla) return MyTypeWithDefault is
begin
return Self.Data;
end Stuff;
end MyProc;
您可以在Ada 2012之前的样式中做到这一点:
generic
type MyTypeWithDefault is private;
Default_Value : in MyTypeWithDefault;
package MyProc is
type Bla is tagged private;
function Stuff (Self : Bla) return MyTypeWithDefault;
private
type Bla is tagged record
Data : MyTypeWithDefault:= Default_Value;
end record;
end MyProc;
但你确实提出了一个很好的观点;应该有某种方法来指定泛型的形式参数中的新方面。