Boo:显式指定哈希类型
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boo 中的内置哈希表与标准 .NET 哈希表非常相似 - 每个条目的键和值都是“对象”类型。
最好的选择是使用通用字典:
此示例将创建一个字典,其中键的类型是字符串,值的类型为 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:
This example will create a dictionary where the type of the key is a string, and the value will be of type Foo
另一个保持代码不变的选项是使用
-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 fromSystem.Collections.Generic
instead of the built-in list and dictionary types, as performance is much better.