Ada 语言 - 如何存储函数返回的字符串值?

Ada language - How to store a string value returned by a function?

我正在尝试在 Ada 2012 中进行一些基本的命令行交互,但我找不到从 Ada.Command_Line.Command_Name() 函数捕获字符串 returned 的方法。

我可以在网上找到的所有示例都只是使用 Put() 打印字符串,而不是先将其存储在局部变量中。这是我试过的错误代码,它确实可以编译,但是当我尝试将 return 字符串值分配给字符串变量时抛出 CONSTRAINT_ERROR ... length check failed...

with Ada.Command_Line; use Ada.Command_Line;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;

procedure Command_Line_Test is
  ArgC : Natural := 0;
  InvocationName : String (1 .. 80);
begin
  ArgC := Argument_Count;
  Put ("Number of arguments provided: ");
  Put (ArgC'Image);
  New_Line;

  InvocationName := Command_Name;  --  CONSTRAINT_ERROR here
  Put ("Name that the executable was invoked by: ");
  Put (InvocationName);
  New_Line;

end Command_Line_Test;

我只是以 Command_Name 为例,但想象一下任何其他函数都可以 return 一个字符串(可能是一个在程序生命周期中会多次更改的字符串),我们应该如何声明局部变量以存储 returned 字符串?

Ada 中的字符串处理与其他编程语言非常不同,当您声明一个 String(1..80) 时,它期望函数返回的字符串实际长度为 1..80,而您的可执行路径(由 Command_Name 返回)可能会更短(或更长)。

你总是可以引入新的声明块并在其中创建一个字符串变量 喜欢这里

with Ada.Command_Line; use Ada.Command_Line;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;

procedure Main is
   ArgC : Natural := 0;

begin
   ArgC := Argument_Count;
   Put ("Number of arguments provided: ");
   Put (ArgC'Image);
   New_Line;
   declare
      InvocationName: String := Command_Name;  --  no more CONSTRAINT_ERROR here
   begin
      Put ("Name that the executable was invoked by: ");
      Put (InvocationName);
      New_Line;
   end;

end Main;

或者您可以使用 Ada.Strings.Unbounded 包

with Ada.Command_Line; use Ada.Command_Line;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure Main is
   ArgC : Natural := 0;
   InvocationName : Unbounded_String;
begin
   ArgC := Argument_Count;
   Put ("Number of arguments provided: ");
   Put (ArgC'Image);
   New_Line;

   InvocationName := To_Unbounded_String(Command_Name);  --  no more CONSTRAINT_ERROR here

   Put ("Name that the executable was invoked by: ");
   Put (To_String(InvocationName));
   New_Line;

end Main;

同意 Timur 的观点,只是没有必要将 Invocation_Name 的声明移动到嵌套的声明块中。

你本来可以写的;

   Invocation_Name : String := Command;

它在原始代码中的位置,在过程 Main 的声明中。

或更好;

   Invocation_Name : constant String := Command;

或者更好的是,完全删除声明并将 Main 的最后 3 行替换为;

   Put_Line ("Name that the executable was invoked by: " & Command);