2014-03-12 67 views
1

我不知道,如果這叫做currying,但是我想要一個HashMap(String,Long,Short,String);Ada HashMap in HashMap

蚋編譯器抱怨:

main.adb:20:13: warning: possible infinite recursion 
main.adb:20:13: warning: Storage_Error may be raised at run time 
main.adb:24:13: warning: possible infinite recursion 
main.adb:24:13: warning: Storage_Error may be raised at run time 

什麼是寫在這裏的「=」功能正確的方法是什麼?

main.adb

with Ada.Containers.Indefinite_Hashed_Maps; 
with Ada.Strings.Hash; 
with Ada.Text_IO; 

procedure Main is 
    subtype Short is Short_Integer; 
    subtype Long is Long_Integer; 

    function Hash (X : String) return Ada.Containers.Hash_Type is 
     (Ada.Strings.Hash (X)); 

    function Hash (X : Short) return Ada.Containers.Hash_Type is 
     (Ada.Containers.Hash_Type'Mod (X)); 

    function Hash (X : Long) return Ada.Containers.Hash_Type is 
     (Ada.Containers.Hash_Type'Mod (X)); 

    package Short_String_Map is new Ada.Containers.Indefinite_Hashed_Maps (Short, String, Hash, "="); 
    function "=" (Left, Right : Short_String_Map.Map) return Boolean is 
     (Left = Right); 

    package Long_Short_Map is new Ada.Containers.Indefinite_Hashed_Maps (Long, Short_String_Map.Map, Hash, "="); 
    function "=" (Left, Right : Long_Short_Map.Map) return Boolean is 
     (Left = Right); 

    package String_Long_Map is new Ada.Containers.Indefinite_Hashed_Maps (String, Long_Short_Map.Map, Hash, "="); 
begin 
    -- Map(String, Long, Short, String); 
    null; 
end Main; 

回答

3

而不是

function "=" (Left, Right : Short_String_Map.Map) return Boolean is 
    (Left = Right); 

function "=" (Left, Right : Short_String_Map.Map) return Boolean is 
    (Short_String_Map."=" (Left, Right)); 

,或者甚至更好(我認爲):

function "=" (Left, Right : Short_String_Map.Map) return Boolean 
    renames Short_String_Map."=";  

問題在於,根據可見性規則,您編寫它的方式=Left = Right中是指您剛纔在上面一行中定義的"="。當然這意味着你已經定義了一個無限遞歸函數。若要使用Short_String_Map中定義的"=",則必須使用類似Short_String_Map."="(Left,Right)的語法來明確指出您所指的是哪個"="