为什么你能不能在拉姆达使用收益,当你可以在一个lambda使用伺机? [英] Why can you not use yield in a lambda, when you can use await in a lambda?

查看:207
本文介绍了为什么你能不能在拉姆达使用收益,当你可以在一个lambda使用伺机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/2009/08/24/iterator-blocks-part-seven-why-no-anonymous-iterators.aspx\">According到埃里克利珀,匿名迭代器并没有加入到语言,因为这将是过于复杂来实现它。

这是跟我没关系,而且直到他们说干就干,匿名实现异步方法,它没有打扰我。编译器必须做同样的事情异步方法,因为它有对迭代器(将其转换成状态机),所以我很困惑,为什么匿名迭代器不允许为好,当匿名异步方法。

有人能阐明这一些轻?


解决方案

  

据埃里克利珀,匿名迭代器并没有加入到语言,因为它会过于复杂,以实现它。


这是不是precisely什么,我想要传达。相关费用实施成本,是的,但它是实现成本的在其中没有设置架构,以实现复杂的功能的现有的编译器。


  

编译器必须做同样的事情异步方法,因为它有对迭代器(将其转换成状态机),所以我很困惑,为什么匿名迭代器不允许为好,当匿名异步方法。


一个简短的历史是相关的。 C#首先必须在C#2.0匿名方法和迭代器块。当我将在C#3.0的lambda它是重构现有的所有匿名方法code,使其能够处理所有的lambda表达式的新功能主要成本。使得它更加复杂和昂贵的修改。使得迭代器块lambda表达式被认为成本过高,将可累积的利益;它本来的总成本的很大比例。 我们买不起。如果您在开发者部门的工作日程加起来每一支球队,球队的最长的极是C#3.0编译器团队,我对语义分析工作是IIRC的在编译器团队竿子最长。每一天,我们可能已经下滑C#3.0,这将是一个一天的Visual Studio将有所下滑。因此任何没有做LINQ更好的被切断,其中包括lambda表达式迭代器

在C#4,迭代lambda表达式都被认为是很多的一个特点。我们有潜在的良好功能的列表字面上比你胳膊长,我们有能力做的比他们的十分之一以下。

在C#5队增加异步方法。设计和实施团队尝试了很长一段时间来与潜在的抽象,是常见的两种迭代器块,等待重写;他们显然类似的,当你注意。但最终,发现一般的解决方案的成本没有收回成本。通用性是出奇的昂贵,并且找到一个一般性的设计只统一的两个的东西是愚蠢的,如果它是不便宜。

因此​​,决定是执行等待重写为自己的事。鉴于球队将要承担这个巨大的成本,而且考虑到的异步方法原始的变换将是为lambda形式,无论如何,决定做出投资于完整的功能:包括lambda表达式,异步lambda表达式异步方法包含lambda表达式,整个交易。该功能的成本是整个特征的成本,这是非常昂贵的一小部分。

再次,我们有极长的问题。这可能已经动摇等待上的λ引擎的任何工作是要避免的,这包括试图使它们与迭代器块工作。

现在比较的Visual Basic。 VB很长一段时间都没有迭代器块。当被添加后,没有现有的基础设施继续工作!整个事情可以从地面建立了以处理包括含有迭代器块lambda表达式和lambda表达式迭代器块,这样做。

C#编译器已被彻底重新设计,并通过罗斯林项目改写。我希望,这将降低在C#中的假想未来的版本中实现迭代器块lambda表达式的成本。我们将拭目以待!

According to Eric Lippert, anonymous iterators were not added to the language because it would be overly complicated to implement it.

That is okay with me, and it didn't bother me until they went ahead and implemented anonymous asynchronous methods. The compiler has to do the same thing for async methods as it has to for iterators (convert them into state machines), so I am very confused why anonymous iterators are not allowed as well, when anonymous async methods are.

Can someone shed some light on this?

解决方案

According to Eric Lippert, anonymous iterators were not added to the language because it would be overly complicated to implement it.

That is not precisely what I intended to convey. The relevant cost is implementation cost, yes, but it is implementation cost in an existing compiler which was not set up architecturally to implement that complex feature.

The compiler has to do the same thing for async methods as it has to for iterators (convert them into state machines), so I am very confused why anonymous iterators are not allowed as well, when anonymous async methods are.

A brief history is relevant. C# first had anonymous methods and iterator blocks in C# 2.0. When I added lambdas in C# 3.0 it was a major cost to refactor all of the existing anonymous method code so that it could handle all the new features of lambdas. That made it even more complicated and expensive to modify. Making iterator block lambdas was judged too costly for the benefits that would be accrued; it would have been a large percentage of the total cost. We could not afford it. If you added up every team in Developer Division's work schedule, the team with the "longest pole" was the C# 3.0 compiler team, and my work on the semantic analyzer was IIRC the longest pole on the compiler team. Every day we might have slipped C# 3.0, that would have been a day that Visual Studio would have slipped. Therefore anything that didn't make LINQ better was cut, and that included iterator lambdas.

In C# 4, iterator lambdas were one feature of many that were considered. We had a list of potential good features literally longer than your arm and we could afford to do less than a tenth of them.

In C# 5 the team added async methods. The design and implementation teams tried for a long time to come up with an underlying abstraction that was common to both the iterator block and await rewrites; they are obviously similar, as you note. But ultimately, the cost of finding the general solution did not pay for itself. Generality is surprisingly expensive, and finding a generality that by design unifies only two things is silly if it is not cheap.

Therefore the decision was made to implement the await rewriter as its own thing. Given that the team was going to take on this large cost, and given that the original transformation of async methods was going to be into a lambda form anyway, the decision was made to invest in the full feature: async methods containing lambdas, async lambdas containing lambdas, the whole deal. The cost of that feature was a small fraction of the cost of the whole feature, which was extremely expensive.

And again, we have a problem with long poles. Any work on the lambda engine that could potentially have destabilized await is to be avoided, and that includes trying to make them work with iterator blocks.

Now compare Visual Basic. VB for a long time had no iterator blocks at all. When they were added, there was no existing infrastructure to keep working! The whole thing could be built from the ground up to handle iterator blocks containing lambdas and lambdas containing iterator blocks, and so that was done.

The C# compiler has been thoroughly rearchitected and rewritten via the Roslyn project. I am hoping that this will lower the cost of implementing iterator block lambdas in a hypothetical future version of C#. We shall see!

这篇关于为什么你能不能在拉姆达使用收益,当你可以在一个lambda使用伺机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆