English 中文(简体)
Boo: Explicitly specifying the type of a hash
原标题:

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?

最佳回答

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

问题回答

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.





相关问题
How stable and mature is Boo?

I had a look at Boo and it looks pretty awesome! I m just curious about how stable it is at this stage? I mean, would you consider using it in real live production code?

How to create nested macro in Boo

I am creating nested macros in Boo, I wrote this program: macro text: macro subMacro: text["Text"] = "Hello World" return [| block: System.Console.WriteLine( "Hello World" ); |]...

Boo: Explicitly specifying the type of a hash

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 it possible to save a dynamic assembly to disk?

I recently bought Ayende s book Building DSLs in Boo (buy it, read it, it s awesome) but I m coming up against an implementation problem and I want to see what the generated code looks like. I would ...

Is WSA compiler in Boo safe to use?

In our program we use boo as macro system about 2 years. All works like a charm, but python syntax is weird for newcomers. As I know boo has white space agnostic (WSA) compiler with ruby like syntax: ...

Boo - Excel Automation, trouble selecting ranges

I m investigating Boo and thought it would be a useful exercise to try converting a couple of venerable VB Scripts that automate Excel (2007, in this instance). A lot of things seem to translate very ...

热门标签