Re: very strange effect with anonymous delegates (.net 2.0)



"cody" <deutronium@xxxxxx> wrote in message
news:OwyyMYY5FHA.2560@xxxxxxxxxxxxxxxxxxxxxxx
> Ok I had a look in reflector and I now see the problem. Since in generated
> code there is no local variable "PropertyInfo prop" but instead just a
> call to "enumerable.Current" so there is nothing to 'lift'.
>
> This maybe right from the view of the ILCode but it is toally wrong from
> the view of the sourcecode since "PropertyInfo prop" is a readonly
> variable from the programmer's view.
>
> Iam the opinion that is is a very dangerous pitfall. If MS won't fix that,
> they should at least throw a compiler error when a foreach iterator is
> used in a anonymous method.
>
> What do you think about that?
>

Surely its simply following standard scoping rules - its declaring the class
at the outermost scope of the local variables that are used within the anon
delegate. It obviously can't do it within the scope as the outermost one
wouldn't be in scope at that point.

So what you are asking for is special casing of your situation (I'm not
lambasting you for this just stating what you are asking). This is an
equally valid construct:

int i = 0;
int[] ii = int[3] { 1, 2, 3};

foreach ( i in ii )
{
// blah
}

this has basically the same scope as your iterator version - what would you
have the compiler do here?

I can understand your frustration, the closure like feature of anon
delegates can lead to some weird behavior if you don't know whats ahppening
behind the scenes. But asking fofr compiler special casig of your construct
is alot to ask (but it has been done before)

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


.



Relevant Pages

  • Re: Reusing the name of local variable
    ... it's not accessible before its declaration. ... Doesn't compiler see that these are two different variables? ... It considers them to be two different local variables with the same ... with one of them in the scope of the other. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Garbage collector quiz
    ... the "tmp" variable is overwritten after the inner loop has completed. ... concept of local variables with a scope smaller than a whole method, ... really be called a translator) there are no named local variables, ... So the Java compiler has used one of the slots to hold ...
    (comp.lang.java.programmer)
  • RE: MSIL Inquiry involving boxing
    ... This means the offical Microsoft provided C# compiler will try to ... The keyword *init* means that the local variables must be ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.framework)
  • Re: Pyserial again
    ... Richie Hindle wrote: ... at this point we're getting into application design issues unless and until Luca can learn enough Python and other programming stuff to understand why "ser" is "going out of scope" ... If you don't know about object construction and destruction, memory allocation and garbage collection, the concept of "scope" and the difference between things like local variables, global variables, and attributes, we're going to continue to have an exceptionally difficult and frustrating time helping you. ...
    (comp.lang.python)
  • Re: To Hungarian or not to Hungarian
    ... prefix that denotes whether the item is a parameter, ... The type, but not the scope. ... I personally do not use prefixes for local variables. ... "Death is one of the few things that can be done as easily lying ...
    (borland.public.delphi.non-technical)