Scala 如何知道“def foo"和“def foo"之间的区别?和“def foo()"? [英] How does Scala know the difference between "def foo" and "def foo()"?
问题描述
我知道scala中空参数和无参数方法的用法区别,我的问题与生成的类文件有关.当我在 javap 中查看这两个类时,它们看起来完全一样:
I know about the usage difference between empty-parameter and parameterless methods in scala, and my question pertains to the class file generated. When I look at these two classes in javap, they look exactly the same:
class Foo {
def bar() = 123;
}
class Foo {
def bar = 123;
}
但是当我在 scalap 中查看它们时,它们准确地反映了参数列表.scalac 在类文件中的哪个位置明确了这种区别?
But when I look at them in scalap, they accurately reflect the parameter lists. Where in the class file is scalac making this distinction known?
推荐答案
在 .class 文件中,有一个名为 ScalaSig
的类文件属性,其中包含 Scala 所需的所有额外信息.它不是人类真正可读的,但它就在那里:
In the .class file, there is a class file attribute called ScalaSig
, which contains all of the extra information needed by Scala. It's not really readable by humans, but it is there:
$ javap -verbose Foo.class
const #195 = Asciz ScalaSig;
const #196 = Asciz Lscala/reflect/ScalaSignature;;
const #197 = Asciz bytes;
const #198 = Asciz ^F^A!3A!^A^B^A^S\t\tb)^[7f^Y^Vtw\r^^5DQ^V^\7.^Z:^K^E\r!^
Q^A^B4jY^VT!!^B^D^B^UM^\^W\r\1tifdWMC^A^H^....
另见 Scala 签名是如何存储的? 和 scala.reflect.ScalaSignature,这是'非常有趣:-)
See also How is the Scala signature stored? and scala.reflect.ScalaSignature, which isn't very interesting :-)
这篇关于Scala 如何知道“def foo"和“def foo"之间的区别?和“def foo()"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!