Lambda反序列化 – Lambda表达式是否始终被转化为Scala的静态方法?

huangapple go评论138阅读模式
英文:

lambda deserilization - does lambda expression always desugared to a static method for Scala?

问题

I noticed that while Java's instance capturing lambda expressions are translated into private instance methods, those for Scala are translated into public static methods which take the containing class as the first argument. Basically, a desugared lambda expression seems to be in the following form:

public static final boolean $anonfun$getLambda$1(com.test.SomeClass):
   ...

Is this always the case for Scala lambda expressions? If not, what will be the cases where they will be instance methods?

Also, why is this the case for Scala lambda expressions, which work differently from Java lambda expressions?

英文:

I noticed that while Java's instance capturing lambda expressions are translated into private instance methods, those for Scala are translated into public static method which takes the containing class as the first argument.
Basically a desugared lambda expression seems to be in the following follow:

public static final boolean $anonfun$getLambda$1(com.test.SomeClass):
   ...

Is this always the case for Scala lambda expressions? If not, what will be the cases where they will be instance methods?

Also, why is the case for Scala lambda expressions which works differently from Java lambda expressions?

答案1

得分: 2

Java也可以创建静态方法;这取决于上下文。

Scala的Lambda函数早于Java的,这解释了它们的实现不同:

Scala可能会尝试复制Java的做法,但当Scala编写时,Java还没有这些功能。

Java不打算复制Scala的做法。此外,Java的语言设计团队与涉及类文件格式和JVM本身的团队直接沟通,因此,对性能的任何担忧都是不相关的 - 如果语言实现创建了一个VM无法处理的情景,那么可以在内部解决这个问题。这意味着Java将以其他托管在JVM上的语言无法做到的方式设计其语言特性。

从实际角度来看,在类级别的静态方法和私有实例方法之间几乎没有什么区别。这基本上是一个无关紧要的细节(在类文件级别,一旦您假设静态方法将第一个参数作为接收者的替代,唯一真正的区别就是动态分派 - 作为一个概念,私有方法不涉及,因此,实际上没有真正的区别:它们都将“接收者”作为第一个参数)。

英文:

Java can also create static methods; it depends on the context.

Scala's lambdas predate java's, this explains why the implementations are different:

Scala would probably attempt to copy what java does, except, when scala was written, java didn't have them yet.

Java has no interest in copying what scala does. In addition, the language design team for java is in direct communication with teams involved in the class file format and the JVM itself, thus, any concerns about performance are irrelevant - if the language implementation creates a scenario that the VM doesn't handle well, then that can be addressed 'in house' so to speak. This means java will design its language features in ways that other hosted-on-the-JVM languages just cannot do.

Pragmatically speaking, there is very little difference between a static method and a private instance method at the class level. A detail that basically just doesn't matter (the only real difference between static and instance methods at the class file level, once you posit that the static method takes as first arg a parameter that is a stand-in for the receiver, is dynamic dispatch - as a concept, that just isn't a thing private methods engage in, therefore, there is no real difference: They both take 'the receiver' as first argument).

huangapple
  • 本文由 发表于 2020年8月14日 09:18:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63405221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定