如何在Boo中创建调度表?(How can I create a dispatch table in Boo?)

我希望能够将函数存储在哈希表中。 我可以创建一个像这样的地图:

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

但是我无法打电话给它:

func = hash["one"] func()

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

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

I'd like to be able to store a function in a hashtable. I can create a map like:

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

But I'm not able to call it:

func = hash["one"] func()

This produces the following error message: It is not possible to invoke an expression on type 'object'. Neither Invoke or Call works.

How can I do it ? From what I'm guessing, the stored function should be cast to something.

最满意答案

你需要强制转换为Callable类型 :

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

You need to cast to a Callable type:

hash = {} hash["one"] = def (): print "one got called" func = hash["one"] as callable func()如何在Boo中创建调度表?(How can I create a dispatch table in Boo?)

我希望能够将函数存储在哈希表中。 我可以创建一个像这样的地图:

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

但是我无法打电话给它:

func = hash["one"] func()

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

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

I'd like to be able to store a function in a hashtable. I can create a map like:

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

But I'm not able to call it:

func = hash["one"] func()

This produces the following error message: It is not possible to invoke an expression on type 'object'. Neither Invoke or Call works.

How can I do it ? From what I'm guessing, the stored function should be cast to something.

最满意答案

你需要强制转换为Callable类型 :

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

You need to cast to a Callable type:

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