Search code examples
constantsprivateadarenaming

How to rename an Ada constant defined in the private part


I want to rename a constant in the public part of a package (the original name is deprecated) that is defined in the private part. I tried this but GNAT says:

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;

Solution

  • Is there a reason you want a rename instead of (say)
    function My_Renamed_Constant return The_Type;
    which simply returns My_Constant in the package body?

    Functionally identical... and should inline if you're worried about speed.

    Later in the deprecation process, make My_Renamed_Constant the constant and My_Constant the function instead. Then, when you think you're ready to retire it, have function My_Constant raise Program_Error or a custom exception indicating "using deprecated constant" to catch any usage you missed.