Micheal J schrieb:
>  Hi,
> 
>>> On May 1, 2008, at 10:02 AM, Johannes Luber wrote:
>>>
>>>> Hi!
>>>>
>>>> I've noticed following new addition to the Token class:
>>>>
>>>> public static final Set legalOptions =
>>>>             new HashSet() {
>>>>                 {
>>>>                 add(defaultOption);
>>>>                 }
>>>>             };
>>>>
>>>> Can someone explain me, what that code snippet actually does? It 
>>>> looks like that a new HashSet is created and that the variable 
>>>> defaultOption is added, but that's a guess as I have never 
>> seen such 
>>>> syntax before. Am I right?
>>>>
>>> The {...} is a ctor for the unnamed anon inner class.  weird, eh?
>>>
>>> Ter
>>>
>> And what for do you intend to use this inner class for? C# 
>> doesn't have a direct equivalent.
>>
>> Johannes
> 
> 
> This translates to something like:
> 
> ... class Token 
> {
>    ...
> 
>    static readonly IDictionary<string,string> legalOptions;
> 
>    ...
> 
>    static Token()
>    {
>       legalOptions = new Dictionary<string,string>();
>       legalOptions.Add(defaultOption, defaultOption);
>    }
> 
>    ...
> }

This is definitively easier to understand than the Java version. Thanks!

> Notes:
> 
> 1. C# has no built-in Set type so you may use a list or dictionary etc. I
> prefer a dictionary as it is faster to search (for set membership tests).
> Just use same value for key and value.
> 
> 2. The static ctor simulates the actions of the anonymous inner class
> 
> 3. If you prefer, you may build a set type for C# (or reuse one from a
> BSD-compatible library)

If I still had to implement this, then I'd take the class from 
<http://www.itu.dk/research/c5/>. But as that is dependent on generics 
I'd have to settle on <http://www.codeproject.com/KB/recipes/sets.aspx>. 
Too bad, that HashSet is only part of .NET 3.5...

Johannes
_______________________________________________
antlr-dev mailing list
[email protected]
http://www.antlr.org:8080/mailman/listinfo/antlr-dev

Reply via email to