簡體   English   中英

如何在Boo中創建調度表?

[英]How can I create a dispatch table in Boo?

我希望能夠在哈希表中存儲一個函數。 我可以創建一個像這樣的地圖:

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

但我無法稱之為:

func = hash["one"]
func()

這將產生以下錯誤消息: 無法在類型'object'上調用表達式 InvokeCall無效。

我該怎么做 ? 根據我的猜測,應該將存儲函數轉換為某些東西。

您還可以使用通用詞典來防止需要轉換為可調用對象:

import System.Collections.Generic

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

fn = hash["one"]
fn()

您需要強制轉換為Callable類型

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM