2009-07-18 23 views
1

我希望能夠將函數存儲在散列表中。我可以創建一個映射,如:如何在Boo中創建一個調度表?

hash = {} 
hash["one"] = def(): 
    print "one got called" 

但我不能把它叫做:

func = hash["one"] 
func() 

這將產生以下錯誤信息:這是不可能的調用類型的表達式「對象'InvokeCall都不起作用。

我該怎麼辦?從我猜測,存儲的功能應該被轉換成某種東西。

回答

2

你需要轉換爲Callable type

hash = {} 
hash["one"] = def(): 
    print "one got called" 

func = hash["one"] as callable 
func() 
+0

謝謝!這工作。 – Geo 2009-07-18 13:04:49

3

你也可以使用一個通用的解釋,以防止需要轉換爲可調用:

import System.Collections.Generic 

hash = Dictionary[of string, callable]() 
hash["one"] = def(): 
    print "got one" 

fn = hash["one"] 
fn() 
相關問題