如何重命名私有部分中定义的 Ada 常量

How to rename an Ada constant defined in the private part

我想重命名私有部分中定义的包的 public 部分中的常量(原来的名称已弃用)。我试过了,但是 GNAT 说:

full constant declaration appears too late

package Sample is

  type The_Type is private;
  My_Constant : constant The_Type;

  My_Renamed_Constant : The_Type;

private

  type The_Type is ...;
  My_Constant : constant The_Type := ...;

  My_Renamed_Constant : The_Type renames My_Constant;

end Sample;

您是否想要重命名而不是(比如)
function My_Renamed_Constant return The_Type;
包体中的哪个简单 returns My_Constant ?

功能相同...如果您担心速度,应该内联。

稍后在弃用过程中,将 My_Renamed_Constant 设为常量,将 My_Constant 改为函数。然后,当您认为您已准备好停用它时,让 function My_Constant 引发 Program_Error 或指示 "using deprecated constant" 的自定义异常以捕获您错过的任何用法。

您可能不需要使用重命名;这样行吗? (这可能完全取决于 The_Type 在您的情况下的完整声明)

package Sample is

  type The_Type is private;
  My_Constant : constant The_Type;

  My_Renamed_Constant : constant The_Type;

private

  type The_Type is new Integer;
  My_Constant : constant The_Type := 42;

  My_Renamed_Constant : constant The_Type := My_Constant;

end Sample;