Ada中子类型的非连续范围? [英] Non-contiguous ranges for subtypes in Ada?
问题描述
作为一种Ada类型系统的学习练习,我试图制作3种类型(或更确切地说,是一种类型和2种亚型):
Totally as an Ada-type-system learning exercise, I was trying to make 3 types (or rather, a type and 2 subtypes):
-
Month_Type
,所有月份的枚举 -
Short_Month_Type
,Month_Type
子类型只有30天的月份 -
February_Month_Type
,只是2月的子类型
Month_Type
, an enumeration of all monthsShort_Month_Type
, aMonth_Type
subtype only having the months with 30 daysFebruary_Month_Type
, a subtype with just February
似乎子类型必须使用 range
机制,正确? (还有其他类型的子类型吗?)为了使其能够使用连续范围,我必须按以下顺序放置 Month_Type
枚举:
It seems that subtypes must use the range
mechanism, correct? (Is there any other kind of subtype?) In order to get it to work with contiguous ranges, I had to put my Month_Type
enumeration in this order:
type Month_Type is (February, April, June, September, November, January, March, May, July, August, October, December);
显然,这不是几个月的自然顺序,我可以看到人们/我都在努力 Month_Type'First
或期望获得一月份的东西。
Obviously this isn't the natural order of months, and I could see people/me trying to do Month_Type'First
or something expecting to get January.
因此,这个愚蠢的例子有两个普遍的问题:
So, two general questions from this silly example:
- 我可以有一个指定基本类型而不是范围的特定子类型的子类型吗?
- 可以我以某种方式隐藏了放置月份的顺序的实现细节(例如,使第一个不可见)?
谢谢!
推荐答案
否,枚举 子类型仅允许 range_constra int ,但您可以创建任意数量的集合使用 Ada.Containers.Ordered_Sets
。在此处和此处。
No, an enumeration subtype only admits a range_constraint in this context, but you could create any number of Sets using Ada.Containers.Ordered_Sets
. There are examples here and here.
这篇关于Ada中子类型的非连续范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!