Return Ada 中只有一个元素的数组
Return an array with one element in Ada
我正在尝试 return 一个只有一个函数元素的数组。我已经尝试了几种口味,但仍然找不到它要我做的事情:
with Ada.Text_IO; use Ada.Text_IO;
procedure Beer_Lists is
type Beer is (Guinness, PBR, Budweiser);
type Beer_List is array (Positive range <>) of Beer;
function Beer_I_Like return Beer_List;
function Beer_I_Like return Beer_List is
begin
-- error: Positional aggregate cannot have one component
-- error: write instead "Beer_List'First => ..."
return (Guinness);
-- error: expected type "Beer_List"
-- error: found type "Beer"
-- return Guinness;
-- error: prefix for "First" attribute must be constrained array
-- return (Beer_List'First => Guinness);
-- error: named association cannot follow positional association
-- error: "=>" expected (positional association cannot followed named association)
-- return (Beer_List'First => Guinness, Guinness);
-- error: named association cannot follow positional association
-- return (Guinness, Beer_List'First => Guinness);
-- error: prefix of attribute must be a name
-- error: qualify expression to turn it into a name
-- return (Guinness)'First => Guinness;
-- error: prefix for "First" attribute must be contrained array
-- error: missing ";"
-- return Beer_List'First => Guinness;
end Beer_I_Like;
begin
for B of Beer_I_Like loop
Put_Line (B'Image);
end loop;
end Beer_Lists;
Return(1 => 吉尼斯); — 请:-)
出于某种原因(我想是为了避免表达式的歧义,但我没有检查),在 Ada 中你可以使用 positional aggregate 形式 (1, 2, 3)
只有值的数量大于 1(记录也是如此)。
解决方案是显式地写入索引(或字段名称),如 (1 => Guiness)
我正在尝试 return 一个只有一个函数元素的数组。我已经尝试了几种口味,但仍然找不到它要我做的事情:
with Ada.Text_IO; use Ada.Text_IO;
procedure Beer_Lists is
type Beer is (Guinness, PBR, Budweiser);
type Beer_List is array (Positive range <>) of Beer;
function Beer_I_Like return Beer_List;
function Beer_I_Like return Beer_List is
begin
-- error: Positional aggregate cannot have one component
-- error: write instead "Beer_List'First => ..."
return (Guinness);
-- error: expected type "Beer_List"
-- error: found type "Beer"
-- return Guinness;
-- error: prefix for "First" attribute must be constrained array
-- return (Beer_List'First => Guinness);
-- error: named association cannot follow positional association
-- error: "=>" expected (positional association cannot followed named association)
-- return (Beer_List'First => Guinness, Guinness);
-- error: named association cannot follow positional association
-- return (Guinness, Beer_List'First => Guinness);
-- error: prefix of attribute must be a name
-- error: qualify expression to turn it into a name
-- return (Guinness)'First => Guinness;
-- error: prefix for "First" attribute must be contrained array
-- error: missing ";"
-- return Beer_List'First => Guinness;
end Beer_I_Like;
begin
for B of Beer_I_Like loop
Put_Line (B'Image);
end loop;
end Beer_Lists;
Return(1 => 吉尼斯); — 请:-)
出于某种原因(我想是为了避免表达式的歧义,但我没有检查),在 Ada 中你可以使用 positional aggregate 形式 (1, 2, 3)
只有值的数量大于 1(记录也是如此)。
解决方案是显式地写入索引(或字段名称),如 (1 => Guiness)