访问常量作为通用访问类型的子类型

问题描述 投票:0回答:1

对于 null 排除,可以定义排除 null 值的访问类型的子类型:

type    Day_Of_Month_Access          is access   Day_Of_Month;
subtype Day_Of_Month_Not_Null_Access is not null Day_Of_Month_Access;

我还没有找到任何用于定义访问类型的子类型来消除该类型的读写可能性的参考,例如(发明的)示例:

type    Day_Of_Month_Access          is access all Day_Of_Month;
subtype Day_Of_Month_Constant_Access is Day_Of_Month_Access with Access_Constant;

这种子类型在当前的 Ada 中不可能吗?为什么会这样?

pointers constants ada subtype
1个回答
0
投票

我花了几天时间研究这个问题,我相信答案是否定的,你不能使用constant关键字来约束子类型。相关部分是:

3.2.2 子类型声明

1    A subtype_declaration declares a subtype of some previously declared type, 
as defined by a subtype_indication.

Syntax
2/3  subtype_declaration ::= subtype defining_identifier is subtype_indication [aspect_specification];
3/2  subtype_indication ::=  [null_exclusion] subtype_mark [constraint]
4    subtype_mark ::= subtype_name
5    constraint ::= scalar_constraint | composite_constraint
6    scalar_constraint ::= range_constraint | digits_constraint | delta_constraint
7    composite_constraint ::= index_constraint | discriminant_constraint

请注意,子类型定义中没有提及“常量”

并且: 3.10 访问类型

1    A value of an access type (an access value) provides indirect access to the 
object or subprogram it designates. Depending on its type, an access value can 
designate either subprograms, objects created by allocators (see 4.8), or more 
generally aliased objects of an appropriate type.

Syntax
2/2    access_type_definition ::= [null_exclusion] access_to_object_definition 
  | [null_exclusion] access_to_subprogram_definition
3      access_to_object_definition ::= access [general_access_modifier] subtype_indication
4      general_access_modifier ::= all | constant
5      access_to_subprogram_definition ::= access [protected] procedure parameter_profile 
  | access [protected] function  parameter_and_result_profile
5.1/2  null_exclusion ::= not null
6/2    access_definition ::= [null_exclusion] access [constant] subtype_mark
  | [null_exclusion] access [protected] procedure parameter_profile
  | [null_exclusion] access [protected] function parameter_and_result_profile

您可以在这里看到 null_exclusion 和常量位于定义的不同部分,因此它们看起来彼此无关。

© www.soinside.com 2019 - 2024. All rights reserved.