2016-04-27 53 views
1

我有一個名爲Linked_List(.ads)封裝下面是它的代碼:無法理解泛型是如何工作的

Generic 
    type T is private; 
package Linked_List is 
    type List is tagged record 
     Data : T; 
    end record; 
end Linked_List; 

,這裏是其中包含的主要功能(主要包裝上的代碼。 ADB)

with Ada.Text_IO; use Ada.Text_IO; 
with Linked_List; 
procedure Main is 
    type ListOfIntegers is new Linked_List(T => Integer); 
begin 
    null; 
end Main; 

我得到這個錯誤:

4:30 subtype mark required in this context 
found "Linked_List" declared at linked_list.ads:3 
found "Linked_List" declared at linked_list.ads:3 
4:41 incorrect constrain for this kind of type 

任何幫助表示讚賞。

回答

5

new Linked_List(T => Integer)定義了一個新的,而不是一個新的類型。你得到的錯誤信息是因爲編譯器認爲你聲明瞭一個類型,所以看到列30上的一個包的名字會讓它感到困惑;它想看到(子)類型的名稱。

4號線改爲

package ListOfIntegers is new Linked_List(T => Integer); 

之後其中有一個類型ListOfIntegers.List,所以你可以寫

My_List : ListOfIntegers.List; 

您可能會發現不必說ListOfIntegers.所有的時間沒有干擾;你可以說

use ListOfIntegers; 

之後,你可以只寫

My_List : List; 

,但它通常被認爲最好不要過分使用(如果你有幾十個「withed」套餐「使用」所有這些使得很難知道你指的是哪一個)。

順便說一句,正常的Ada用法是使用下劃線分隔標識符中的單詞:List_Of_Names