[英]Size of array type in Ada
我離開了很長一段時間后才回到Ada,所以這可能是一個初學者的問題。 本質上,我正在嘗試打印一維數組的長度。 該數組在記錄內。 如果創建記錄的實例,則可以很好地打印類型,但是我覺得必須有一種方法可以僅從類型中打印長度。 這是我認為可以工作的一些非常人為的示例代碼:
with Ada.Text_Io;
procedure TestApp is
type int_array is array (integer range <>) of integer;
type item_type is record
ia : int_array (0 .. 20);
end record;
begin
Ada.Text_Io.Put_Line(Integer'image(item_type.ia'length));
end TestApp;
但出現錯誤“所選組件'item_type'中的前綴無效”。 如果我實例化item_type並從中獲取范圍,它當然可以正常工作,但我覺得我一定會缺少一些東西。
謝謝
這個:
ia : int_array (0 .. 20);
是匿名數組的子類型,獲取匿名數組長度的唯一方法是通過對象(因為沒有名稱可以指定類型)。 但是,您可以顯式聲明數組子類型(命名子類型):
with Ada.Text_Io;
procedure TestApp is
type int_array is array (integer range <>) of integer;
subtype sub_int_array is int_array(1..20);
type item_type is record
ia : sub_int_array;
end record;
begin
Ada.Text_Io.Put_Line(Integer'image(sub_int_array'length));
end TestApp;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.