[英]Ada language - How to store a string value returned by a function?
I'm attempting to some basic command-line interaction in Ada 2012 and I can't find a way to capture the string returned from the Ada.Command_Line.Command_Name() function. 我正在尝试在Ada 2012中进行一些基本的命令行交互,但我找不到捕获从Ada.Command_Line.Command_Name()函数返回的字符串的方法。
All the examples I can find online simply print the String using Put() without first storing it in a local variable. 我在网上找到的所有例子都只是使用Put()打印String而不先将其存储在局部变量中。 Here's the faulty code I've tried, which does compile but throws a
CONSTRAINT_ERROR ... length check failed
when I try to assign the return String value to the String variable... 这是我尝试过的错误代码,当我尝试将返回String值分配给String变量时,它会编译但抛出
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;
I'm just using Command_Name as an example, but imagine it was any other function that could return a string (perhaps a string that would change multiple times during the program's life), how should we declare the local variable in order to store the returned string? 我只是使用Command_Name作为示例,但想象它是任何其他函数可以返回一个字符串(也许是一个在程序生命期间会多次改变的字符串),我们应该如何声明局部变量以存储返回的串?
String handling in Ada is very different from other programming languages and when you declare a String(1..80) it expect that string returned by a function will actually have length 1..80 while your executable path (which is returned by Command_Name) may be a bit shorter (or longer). Ada中的字符串处理与其他编程语言非常不同,当您声明一个String(1..80)时,它期望函数返回的字符串实际上具有长度1..80而您的可执行路径(由Command_Name返回)可能会更短(或更长)。
You may always introduce new declare block and create a String variable inside of it like here 您可能总是引入新的声明块并在其中创建一个String变量,就像这里一样
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;
Or you may use Ada.Strings.Unbounded package 或者您可以使用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;
Agree with Timur except that there is no need to move the declaration of Invocation_Name into a nested declare block. 同意Timur,除了不需要将Invocation_Name的声明移动到嵌套的声明块中。
You could have just written; 你本来可以写的;
Invocation_Name : String := Command;
Where it was in the original code, in the declarations of procedure Main. 它在原始代码中的位置,在程序Main的声明中。
or better yet; 还是更好;
Invocation_Name : constant String := Command;
or better yet, eliminate the declaration altogether and replace the last 3 lines of Main with; 或者更好的是,完全取消声明并替换Main的最后3行;
Put_Line ("Name that the executable was invoked by: " & Command);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.