views:

137

answers:

4

Is there any particular reason? Is it not possible at all or is it just not implemented yet? Maybe there are any third-party addins that allow lambda evaluations?

UPDATE:

I've found this project on codeplex Extended Immediate Window. Seems that it has been abandoned for some time, but this can be a proof of a concept. Does anybody know any other immediate window extension addins? The ones that can run for/foreach statements in C# for instance?

A: 

I assume, that because it is lazy evaluation, the immediate window cannot know beforehand what values, the captured variables (closure), should have.

Aggelos Mpimpoudis
When writing an expression in the immediate window (or in the watch window) the execution is stopped and the debugger in some scope, so it has access to variables in that scope. Why can't it just pull the variables from there?
Max
@Max - because the very act of "capturing" them changes their nature; they are no longer variables (locals) - they are fields on a compiler-generated class.
Marc Gravell
Exactly, I agree with Max (though I don't understand the minus). Anyway. In ms connect one, can read an interesting conversation featuring Mads (https://connect.microsoft.com/VisualStudio/feedback/details/472999/ability-to-evaluate-lambda-expressions-in-immediate-window?wa=wsignin1.0). Check it!
Aggelos Mpimpoudis
+3  A: 

Well, I think it's because the immediate window can only evaluate expressions, or rather it can only do invocations and assignments. To evaluate a Lambda expression a closure would have to be created for that lambda, typchecked and then executed.

I think it comes down to that the Immediate window is just an evaluator and not an interpreter.

http://msdn.microsoft.com/en-us/library/f177hahy(VS.80).aspx

"The Immediate window is used at design time to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging."

So in effect, your question boils down to why you can't define functions in the immediate window (since lambdas are just annonymous functions), and the answer I think is that it simply wasn't designed for that.

Phyx
Actually it's the other way around. The immediate window evaluates expressions but it does so in a way that's much more accurately described as interpreting.
JaredPar
+3  A: 

When writing a lambda, the act of capturing variables significantly alters the construction of the underlying code (moving variables into fields of compiler-generated classes, that could very easily themselves be chained closure-contexts).

Not even considering the general complexity of doing this, it would then have two choices:

  • capture all the variable values as constants; feasible and pretty simple, but could easily mean that the result of executing in the immediate window is very different to the result of executing in the main body (very undesirable)
  • rewrite the entire code (for the reasons outlined above) on the fly (at a guess, impossible)

Given a choice between "undesirable" and "impossible", I guess they simply chose not to implement a feature that would be inherently brittle, and very complex to write.

Marc Gravell
Hacky implementation: If you detect a lambda, programmatically paste it into the code on the line where the execution stopped, execute that line, delete the lambda, display the output of the lambda. My intuition is for every line of code entered into the immediate window to be executed in this fashion (or rather, in something morally equivalent to this fashion). I don't feel like you've explained why this fails.
Brian
@Brian - you can't possibly do that and continue execution. A lambda that captures *any* of the variables etc forces the IL to be **very** different. And a lambda that *doesn't* talk to any of the variables probably isn't very interesting to start with.
Marc Gravell
+6  A: 

JaredPar of Microsoft wrote a couple of blog posts answering your question: part 1 and part 2. You'll find the answers there.

Omer Raviv