Boo:显式指定哈希类型

发布于 2024-08-20 01:19:54 字数 226 浏览 5 评论 0原文

我是 Boo 的新手,并试图弄清楚如何声明哈希的类型。当我这样做时:

   myHash = {}
   myHash[key] = value
   (later)
   myHash[key].method()

编译器抱怨“方法不是对象的成员”。我认为它不知道哈希中的值是什么类型。

有什么方法可以向编译器声明哈希的键和值的类型,这样它就不会抱怨吗?

I am new to Boo, and trying to figure out how to declare the type of a hash. When I do:

   myHash = {}
   myHash[key] = value
   (later)
   myHash[key].method()

the compiler complains that "method is not a member of object". I gather that it doesn't know what type the value in the hash is.

Is there any way I can declare to the compiler what type the keys and values of the hash are so that it won't complain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒浓于脸红 2024-08-27 01:19:54

boo 中的内置哈希表与标准 .NET 哈希表非常相似 - 每个条目的键和值都是“对象”类型。

最好的选择是使用通用字典:

import System.Collections.Generic

myHash = Dictionary[of string, Foo]()

此示例将创建一个字典,其中键的类型是字符串,值的类型为 Foo

The builtin hashtable in boo is very similar to the standard .NET Hashtable - the key and value of each entry are both of type "object".

Your best bet is to use a generic Dictionary:

import System.Collections.Generic

myHash = Dictionary[of string, Foo]()

This example will create a dictionary where the type of the key is a string, and the value will be of type Foo

○愚か者の日 2024-08-27 01:19:54

另一个保持代码不变的选项是使用 -ducky 开关启用鸭子类型。我个人专门使用 System.Collections.Generic 中的集合,而不是内置列表和字典类型,因为性能要好得多。

Another option that leaves your code unchanged is to enable duck typing with the -ducky switch. I personally exclusively use the collections from System.Collections.Generic instead of the built-in list and dictionary types, as performance is much better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文