如何在Boo中创建调度表?

时间:2009-07-18 09:37:18

标签: .net boo dispatch-table

我希望能够在哈希表中存储一个函数。我可以创建一个像:

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

但是我无法打电话给它:

func = hash["one"]
func()

这会产生以下错误消息:无法在类型“object”上调用表达式。 InvokeCall都无效。

我该怎么办?根据我的猜测,存储的函数应该转换为某种东西。

2 个答案:

答案 0 :(得分:3)

您还可以使用通用词典来防止转换为可调用的:

import System.Collections.Generic

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

fn = hash["one"]
fn()

答案 1 :(得分:2)

您需要转换为Callable type

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

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