Scala 闭包在 Scala.NET 中是如何实现的?
据我了解,Scala 中的 Function 类型会编译为 FunctionN 的实例。例如,这个示例
List(1,2,3).filter(_ >= 2)
意味着
List(1,2,3).filter(new Function1[Int,Bool]{def apply(_$1:Int) = _$1 >= 2;})
How is this Implement in Scala.NET?据我了解,.NET 没有语句级匿名类。而上面的解决方案依赖于匿名类的存在。
As I understand it, Function types in Scala compile to instances of FunctionN. So that for example this example
List(1,2,3).filter(_ >= 2)
means
List(1,2,3).filter(new Function1[Int,Bool]{def apply(_$1:Int) = _$1 >= 2;})
How is this implemented in Scala.NET? As I understand it, .NET does not have statement-level anonymous classes. And the above solution depends on there being anonymous classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 Scala 一无所知,但我不明白为什么不应该像 C# 闭包那样实现它,即以下代码:
该代码是通过在当前类中创建一个新函数来实现的。如果您确实创建了一个闭包:
这将通过创建一个包含变量
max
以及匿名函数的新类来实现。编辑:
我刚刚尝试使用 Scala.Net 编译您的代码,并在 Reflector 中查看编译后的代码,结果如下:
其中
$anonfun$1
是一个实现的类Function1
接口及其apply()
函数如下所示:I don't know anything about Scala, but I don't see why that shouldn't be implemented the same way as C# closures, i.e. the following code:
This code is implemented by creating a new function in the current class. If you really created a closure:
That would be implemented by creating a new class that contains the variable
max
along with the anonymous function.EDIT:
I just tried compiling your code using Scala.Net and looking at the compiled code in Reflector gives this:
Where
$anonfun$1
is a class that implements theFunction1
interface and itsapply()
function looks like this: