在Ada中返回有限类型
我试图通过构造函数返回一个受限类型。我明白,因为这是一个有限的类型,我不能复制的类型,但我不知道最好的方式做它。我使用扩展的return语句使它工作,但有人告诉我,如果没有它,我应该能够返回有限的类型 thing_.ads:在Ada中返回有限类型,ada,Ada,我试图通过构造函数返回一个受限类型。我明白,因为这是一个有限的类型,我不能复制的类型,但我不知道最好的方式做它。我使用扩展的return语句使它工作,但有人告诉我,如果没有它,我应该能够返回有限的类型 thing_.ads: package Thing_Protected is type Thing is protected interface; procedure Verb_It (Object : in out Thing; Key : String) is abstract;
package Thing_Protected is
type Thing is protected interface;
procedure Verb_It (Object : in out Thing; Key : String) is abstract;
function Create return Thing'Class;
private
protected type Thing_Impl is new Thing with
overriding procedure Verb_It (Key : String);
private
Data : Integer;
end Thing_Impl;
end Thing_Protected;
thing_protected.adb:
package body Thing_Protected is
function Create return Thing'Class is
begin
-- Not sure how to make this work:
-- return Thing_Impl'(Data=><>, others=><>);
-- thing_protected.adb:6:35: expected type "Thing_Impl" defined at thing_protected.ads:10
-- thing_protected.adb:6:35: found a composite type
-- extended return:
-- return X : Thing_Impl do
-- null;
-- end return;
-- shortened version:
return X : Thing_Impl;
end;
protected body Thing_Impl is
overriding procedure Verb_It (Key : String) is
begin
null;
end;
end Thing_Impl;
end Thing_Protected;
嗯,那么你想初始化数据?您可以使用泛型/包来实现这一点。。。它有点长,也许有点复杂
package Thing_Protected is
type Thing is protected interface;
procedure Verb_It (Object : in out Thing; Key : String) is abstract;
function Create return Thing'Class;
private
generic
Default : in Integer;
package Implementation is
protected type Thing_Impl is new Thing with
procedure Verb_It (Key : String);
private
Data : Integer:= Default;
end Thing_Impl;
Function Create return Thing'Class;
end Implementation;
end Thing_Protected;
package body Thing_Protected is
package body Implementation is
protected body Thing_Impl is
overriding procedure Verb_It (Key : String) is
begin
null;
end;
end Thing_Impl;
function Create return Thing'class is
begin
return Result : Thing_Impl do
null;
end return;
end Create;
end Implementation;
function K( Data_Val : Integer := 10 ) return Thing'Class is
Package I is new Implementation( Default => Data_Val );
begin
return X : Thing'Class := I.Create do
null;
end return;
end K;
function Create return Thing'Class is ( K );
end Thing_Protected;
嗯,那么你想初始化数据?您可以使用泛型/包来实现这一点。。。它有点长,也许有点复杂
package Thing_Protected is
type Thing is protected interface;
procedure Verb_It (Object : in out Thing; Key : String) is abstract;
function Create return Thing'Class;
private
generic
Default : in Integer;
package Implementation is
protected type Thing_Impl is new Thing with
procedure Verb_It (Key : String);
private
Data : Integer:= Default;
end Thing_Impl;
Function Create return Thing'Class;
end Implementation;
end Thing_Protected;
package body Thing_Protected is
package body Implementation is
protected body Thing_Impl is
overriding procedure Verb_It (Key : String) is
begin
null;
end;
end Thing_Impl;
function Create return Thing'class is
begin
return Result : Thing_Impl do
null;
end return;
end Create;
end Implementation;
function K( Data_Val : Integer := 10 ) return Thing'Class is
Package I is new Implementation( Default => Data_Val );
begin
return X : Thing'Class := I.Create do
null;
end return;
end K;
function Create return Thing'Class is ( K );
end Thing_Protected;
对于受保护类型或任务类型,没有聚合语法(
returnthing\u Impl'(…)
)。所以我认为你必须使用延期退货。(顺便说一句,动词体上的重写
是非法的,即使编译器错误地接受了它。)参见相关示例。ajb:我认为重写
在这里是完全合法的。根据,一个受保护的\u操作\u声明
可以是一个子程序\u声明
,它可以有覆盖
指示符。@flyx请看。我最初声称这在没有扩展返回的情况下是可能的,但正如ajb指出的,这里不能使用聚合(我不知道),因此,扩展返回实际上是正确的解决方案。对于受保护类型或任务类型,没有聚合语法(returnthing\u Impl'(…)
)。所以我认为你必须使用延期退货。(顺便说一句,动词体上的重写
是非法的,即使编译器错误地接受了它。)参见相关示例。ajb:我认为重写
在这里是完全合法的。根据,一个受保护的\u操作\u声明
可以是一个子程序\u声明
,它可以有覆盖
指示符。@flyx请看。我最初声称这在没有扩展返回的情况下是可能的,但正如ajb指出的,这里不能使用聚合(我不知道),因此,延长回报率实际上是正确的解决方案。