为什么不能在立即窗口中计算 lambda 表达式?
有什么特别的原因吗?是根本不可能还是只是还没有实施?也许有任何第三方插件允许 lambda 评估?
更新:
我在 codeplex 扩展立即窗口上找到了这个项目。似乎它已经被放弃了一段时间,但这可以作为一个概念的证明。有人知道任何其他即时窗口扩展插件吗?例如,可以在 C# 中运行 for/foreach 语句吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Microsoft 的 JaredPar 撰写了几篇博客文章来回答您的问题:第 1 部分 和 第 2 部分。你会在那里找到答案。
JaredPar of Microsoft wrote a couple of blog posts answering your question: part 1 and part 2. You'll find the answers there.
编写 lambda 时,捕获变量的行为显着改变了底层代码的构造(将变量移动到编译器生成的类的字段中,这些类本身很容易被链式闭包上下文)。
即使不考虑这样做的一般复杂性,它也会有两种选择:
考虑到“不受欢迎”和“不可能”之间的选择,我猜他们只是选择不实现本质上脆弱的功能,而且写起来非常复杂。
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:
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.
嗯,我认为这是因为立即窗口只能计算表达式,或者更确切地说,它只能执行调用和赋值。要计算 Lambda 表达式,必须为该 Lambda 创建一个闭包,进行类型检查,然后执行。
我认为归根结底,立即窗口只是一个评估器,而不是一个解释器。
http://msdn.microsoft.com/en-us /library/f177hahy(VS.80).aspx
“立即窗口在设计时用于调试和计算表达式、执行语句、打印变量值等。它允许您输入要计算的表达式或在调试期间由开发语言执行。”
所以实际上,你的问题归结为为什么你不能在立即窗口中定义函数(因为 lambda 只是匿名函数),而我认为的答案是它根本不是为此设计的。
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.
如果您仍然需要使用 Visual Studio 2013,您实际上可以使用包管理器控制台窗口在立即窗口中编写循环或 lambda 表达式。就我而言,我在函数顶部添加了一个列表:
我的 GetAll() 函数在哪里:
在这里我不断收到以下错误,所以我想打印出各个存储库中的所有项目:
然后,我发现通过在立即窗口中执行以下命令,部门存储库中有多少记录:
返回 243。
因此,如果您在包管理器控制台中执行以下命令,它将打印出所有项目:
该想法的作者可以在这里找到: http://ogresoft.blogspot.ca /2013/06/how-to-write-loop-or-lambda-expression.html
If you still need to use Visual Studio 2013, you can actually write a loop, or lambda expression in the immediate window using also the package manager console window. In my case, I added a list at the top of the function:
Where my GetAll() function is:
Here I kept getting the following error, so I wanted to print out all the items in the various repositories:
Then, I find out how many records are in the department repository by executing this in the immediate window:
Which returned 243.
So, if you execute the following in the package manager console, it prints out all the items:
The author for the idea can be found here: http://ogresoft.blogspot.ca/2013/06/how-to-write-loop-or-lambda-expression.html
我假设,因为它是惰性求值,所以直接窗口无法事先知道捕获的变量(闭包)应该具有什么值。
I assume, that because it is lazy evaluation, the immediate window cannot know beforehand what values, the captured variables (closure), should have.