题
我想能够存储一个函数在哈希表中。我可以创建一个地图,如:
hash = {}
hash["one"] = def():
print "one got called"
但我不能把它叫做:
func = hash["one"]
func()
这产生以下错误消息:这是不可能的调用的表达式上型“对象” 。既不Invoke
或Call
作品。
我该怎么办呢?从我猜,存储功能应投的东西。
解决方案
您需要转换到可赎回类型:
hash = {}
hash["one"] = def ():
print "one got called"
func = hash["one"] as callable
func()
其他提示
您也可以使用一个通用的字典,以防止需要转换为可调用:
import System.Collections.Generic
hash = Dictionary[of string, callable]()
hash["one"] = def():
print "got one"
fn = hash["one"]
fn()
不隶属于 StackOverflow