gpt4 book ai didi

generics - Ada 通用包扩展

转载 作者:行者123 更新时间:2023-12-02 18:57:59 25 4
gpt4 key购买 nike

我有一个通用包Containers.Stacks,看起来大致像

generic
type Value_Type is private;
package Containers.Stacks with Preelaborate is

--Node stuff

type Stack is new Container with private;

--Other Stack stuff
--Private stuff
end Containers.Stacks;

一切(似乎)都运行得很好。我想重用此实现来创建扩展但用途更窄的堆栈。我提出了以下建议:

with Containers.Stacks;

generic
type Value_Type is range <>;
package Containers.Integer_Stacks with Preelaborate is

package Stacks is new Containers.Stacks(Value_Type);
use Stacks;

type Stack is new Stacks.Stack with null record;

procedure Add(Self : in out Stack);

procedure Subtract(Self : in out Stack);

--So on

end Containers.Integer_Stacks;

重点是,我不想只为特定的整数堆栈重新实现所有常见的堆栈内容。这可行,但留下了 Containers.Integer_Stacks.Stack 和 Containers.Integer_Stacks.Stacks.Stack 的丑陋和奇怪的困惑,我想避免这种情况。然而,尝试将包初始化放在私有(private)部分中也会隐藏所有子例程。是否有更合适的方法来完成我想要做的事情,或者用户是否必须处理存在的同级包初始化?

替代方法:我发现可以定义一个名为 Containers.Stacks.IntegerContainers.Stacks 子包,如下所示:

generic
with function "+"(Left, Right: Value_type) return Value_Type is <>;
package Containers.Stacks.Integer is
procedure Add(Self : in out Stack);
end Containers.Stacks.Integer;

这有效。只是现在我无法调用 Stack.Add,我被迫调用 Add(Stack)

最佳答案

你可以看看

with Containers.Stacks;
generic
type Value_Type is range <>;
with package Basis_Stack is new Containers.Stacks (Value_Type => Value_Type);
package Containers.Integer_Stacks with Preelaborate is
type Stack is new Basis_Stack.Stack with private;
...

这确实需要您的用户自己适本地实例化 Containers.Stacks。

关于generics - Ada 通用包扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42746191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com