2014-03-12 57 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)的语法来明确指出您所指的是哪个"="