kotlin plus 运算符的函数定义在哪里?
Where is function definition of kotlin plus operator?
我只是在 kotlin 源代码中查看 Primitives.kt 文件的源代码,以查看 'plus' 运算符的功能代码。
/** Adds the other value to this value. */
public operator fun plus(other: Int): Int
谁能帮我指导这个函数的代码在哪里?
Primitives.kt
中的这些函数是作为编译器内部函数实现的。这意味着当编译器遇到这样的函数调用时,它会将其替换为一些预定义的较低级别代码序列或对另一个函数的调用。
例如Kotlin/JVM,函数调用Int.plus(Int): Int
在这段代码
fun test(x: Int, y: Int) {
x.plus(y)
}
被替换成类似这样的字节码序列:
ILOAD 0
ILOAD 1
IADD
以及将内部函数调用替换为另一个函数调用的示例:
fun test(x: Int, y: Int) {
x.compareTo(y)
}
编译为:
ILOAD 0
ILOAD 1
INVOKESTATIC kotlin/jvm/internal/Intrinsics.compare (II)I
这里调用的是静态函数Instrincs.compare(Int, Int)
,而不是成员函数Int.compareTo(Int)
。
我只是在 kotlin 源代码中查看 Primitives.kt 文件的源代码,以查看 'plus' 运算符的功能代码。
/** Adds the other value to this value. */
public operator fun plus(other: Int): Int
谁能帮我指导这个函数的代码在哪里?
Primitives.kt
中的这些函数是作为编译器内部函数实现的。这意味着当编译器遇到这样的函数调用时,它会将其替换为一些预定义的较低级别代码序列或对另一个函数的调用。
例如Kotlin/JVM,函数调用Int.plus(Int): Int
在这段代码
fun test(x: Int, y: Int) {
x.plus(y)
}
被替换成类似这样的字节码序列:
ILOAD 0
ILOAD 1
IADD
以及将内部函数调用替换为另一个函数调用的示例:
fun test(x: Int, y: Int) {
x.compareTo(y)
}
编译为:
ILOAD 0
ILOAD 1
INVOKESTATIC kotlin/jvm/internal/Intrinsics.compare (II)I
这里调用的是静态函数Instrincs.compare(Int, Int)
,而不是成员函数Int.compareTo(Int)
。