gpt4 book ai didi

ada - 在 Ada 中将空枚举传递给泛型的惯用方法

转载 作者:行者123 更新时间:2023-12-05 09:31:57 28 4
gpt4 key购买 nike

我正在实例化一个带有枚举的通用包,以访问多个值之一并在子程序重载中使用。我想要一组定义明确、编译时检查过的值,我可以使用和查找。

generic
-- Different types because we don't want to ensure we never put
-- beer in a wine class, or wine in a beer stein. Our inventory
-- never changes, for... reasons.
type Wine is (<>);
type Beer is (<>);
package Bar is
type Wine_Glass is null record;
type Beer_Stein is null record;

-- Unopened cases/bottles of each.
type Wine_Inventory is array (Wine) of Natural;
type Beer_Inventory is array (Beer) of Natural;

procedure Pour (G : in out Wine_Glass; W : in Wine);
procedure Pour (S : in out Beer_Stein; B : in Beer);
end Bar;

描述空枚举的惯用语是什么?

with Bar;
procedure Wine_Tasting is
type Sampling_Wine is (Tempranillo, Zinfandel, Merlot);
pragma Unreferenced (Tempranillo, Zinfandel, Merlot);

type No_Beer is (None);
package Wine_Tasting_Bar is new Bar(Wine => Sampling_Wine, Beer => No_Beer);
Stein : Wine_Tasting_Bar.Beer_Stein;
begin
Wine_Tasting_Bar.Pour (Stein, None); -- legal!
end Wine_Tasting;

有没有一种方法可以这样描述 Beer 是一个没有值的枚举,这样 Pour 就永远不会被 Beer 调用?

最佳答案

你必须声明一个至少有两个值的枚举类型,然后声明一个没有值的子类型。您使用子类型来实例化泛型:

type Wine_Kind is (Red, White, Green);
type Beer_Base is (Ale, Lager);
subtype No_Beer is Beer_Base range Lager .. Ale;

package Wine_Bar is new Bar (Wine => Wine_Kind, Beer => No_Beer);

关于ada - 在 Ada 中将空枚举传递给泛型的惯用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68520702/

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