博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lambda表达式和匿名方法中不支持yield return
阅读量:4680 次
发布时间:2019-06-09

本文共 2437 字,大约阅读时间需要 8 分钟。

尝试写过,但是得到了编译错误:"The yield statement cannot be used inside an anonymous method or lambda expression". 我想不出不支持的理由,最终在网上找到了解释():

    Good question. I would love to have anonymous iterator blocks. It would be totally awesome to be able to build yourself a little sequence generator in-place that closed over local variables. The reason why not is straightforward: the benefits don't outweigh the costs. The awesomeness of making sequence generators in-place is actually pretty small in the grand scheme of things and nominal methods do the job well enough in most scenarios. So the benefits are not that compelling.

    The costs are large. Iterator rewriting is the most complicated transformation in the compiler, and anonymous method rewriting is the second most complicated. Anonymous methods can be inside other anonymous methods, and anonymous methods can be inside iterator blocks. Therefore, what we do is first we rewrite all anonymous methods so that they become methods of a closure class. This is the second-last thing the compiler does before emitting IL for a method. Once that step is done, the iterator rewriter can assume that there are no anonymous methods in the iterator block; they've all be rewritten already. Therefore the iterator rewriter can just concentrate on rewriting the iterator, without worrying that there might be an unrealized anonymous method in there.

    Also, iterator blocks never "nest", unlike anonymous methods. The iterator rewriter can assume that all iterator blocks are "top level".

    If anonymous methods are allowed to contain iterator blocks, then both those assumptions go out the window. You can have an iterator block that contains an anonymous method that contains an anonymous method that contains an iterator block that contains an anonymous method, and... yuck. Now we have to write a rewriting pass that can handle nested iterator blocks and nested anonymous methods at the same time, merging our two most complicated algorithms into one far more complicated algorithm. It would be really hard to design, implement, and test. We are smart enough to do so, I'm sure. We've got a smart team here. But we don't want to take on that large burden for a "nice to have but not necessary" feature. -- Eric

大致意思就是说,要重写编译器以支持在Lamdba表达式或者匿名方法中进行yield return,工作非常复杂,而且成本很高,远不及其带来的好处。不知道这个解释是什么意思,个人感觉将来还是会支持的。目前来说,其实也不构成太大的不便,只是不能写特别ws的代码罢了。

转载于:https://www.cnblogs.com/Gildor/archive/2009/09/05/1560650.html

你可能感兴趣的文章
mysql 左连接 左外连接吗_什么是左外连接?左外连接在工作表查询中的应用
查看>>
python sum函数导入list_python sum函数iterable参数为二维list,start参数为“[]”该如何理解...
查看>>
docker 删除多余镜像_多余Basedisk删除和vDisk镜像反转技术简介
查看>>
mysqlin会使用索引吗_被面试官虐了,索引为何使用B+树,你知道吗
查看>>
mysql8单节点500m_Kubernetes 部署 Mysql 8.0 数据库(单节点)
查看>>
mysql数据工厂生产_MySQL超时与天蓝色数据工厂副本
查看>>
python缩进可以用在任何语句之后_每天一道Python选择题--python缩进
查看>>
mysql查询左边大于左边_MySQL WHERE 子句
查看>>
java 获取颜色_java关于照片属性的获取,颜色模式
查看>>
session丢失原因 java_session没有过期,其保存的数据无故丢失的原因
查看>>
java pkcs 11 write_java pkcs#11读取证书加解密(初学-分享)
查看>>
tranisant java_java tranisant
查看>>
java ibatis 存储过程_ibatis 调用存储过程
查看>>
java中的softreference_Java语言中内存优化的SoftReference 和 WeakReference的对比分析
查看>>
java提供了丰富的类库_Java优势有哪些?
查看>>
java 过滤器权限控制_JAVA过滤器,实现登陆权限限制
查看>>
设计模式java 模板模式_java设计模式--模板方法模式
查看>>
中缀转后缀 java_Java 利用堆栈将中缀表达式转换成后缀
查看>>
java执行sql解析_java执行SQL语句实现查询的通用方法详解
查看>>
java中keepalived开启方式_高可用之KeepAlived(一):基本概念和配置文件分析
查看>>