访问函数';s Kotlin中的默认参数值
函数的默认参数值可以从函数扩展或其他任何地方访问吗访问函数';s Kotlin中的默认参数值,kotlin,default-value,default-parameters,Kotlin,Default Value,Default Parameters,函数的默认参数值可以从函数扩展或其他任何地方访问吗 fun DieRoll.cheatRoll():Int = roll(min = max -1) fun roll(min: Int = 1, max: Int = 6): Int = (min..max).rand() 不,这是不可能的。默认值不可访问。它们仅包含在字节码中的桥接方法中: fun test(a: Int = 123) { } fun test2() { test() test(100) } public
fun DieRoll.cheatRoll():Int = roll(min = max -1)
fun roll(min: Int = 1, max: Int = 6): Int = (min..max).rand()
不,这是不可能的。默认值不可访问。它们仅包含在字节码中的
桥接方法中
:
fun test(a: Int = 123) {
}
fun test2() {
test()
test(100)
}
public final test(int arg0) { //(I)V
<localVar:index=0 , name=this , desc=Lorg/guenhter/springboot/kt/Fun;, sig=null, start=L1, end=L2>
<localVar:index=1 , name=a , desc=I, sig=null, start=L1, end=L2>
L1 {
return
}
L2 {
}
}
public static bridge test$default(org.guenhter.springboot.kt.Fun arg0, int arg1, int arg2, java.lang.Object arg3) { //(Lorg/guenhter/springboot/kt/Fun;IILjava/lang/Object;)V
iload2 // reference to arg2
iconst_1
iand
ifeq L1
L2 {
bipush 123 // <-- THIS IS YOUR DEFAULT VALUE
istore1 // reference to arg1
}
L1 {
aload0 // reference to arg0
iload1 // reference to arg1
invokevirtual org/guenhter/springboot/kt/Fun test((I)V);
return
}
}
public final test2() { //()V
<localVar:index=0 , name=this , desc=Lorg/guenhter/springboot/kt/Fun;, sig=null, start=L1, end=L2>
L1 {
aload0 // reference to self
iconst_0
iconst_1
aconst_null
invokestatic org/guenhter/springboot/kt/Fun test$default((Lorg/guenhter/springboot/kt/Fun;IILjava/lang/Object;)V);
}
L3 {
aload0 // reference to self
bipush 100
invokevirtual org/guenhter/springboot/kt/Fun test((I)V);
}
L4 {
return
}
L2 {
}
}
字节码中的结果:
fun test(a: Int = 123) {
}
fun test2() {
test()
test(100)
}
public final test(int arg0) { //(I)V
<localVar:index=0 , name=this , desc=Lorg/guenhter/springboot/kt/Fun;, sig=null, start=L1, end=L2>
<localVar:index=1 , name=a , desc=I, sig=null, start=L1, end=L2>
L1 {
return
}
L2 {
}
}
public static bridge test$default(org.guenhter.springboot.kt.Fun arg0, int arg1, int arg2, java.lang.Object arg3) { //(Lorg/guenhter/springboot/kt/Fun;IILjava/lang/Object;)V
iload2 // reference to arg2
iconst_1
iand
ifeq L1
L2 {
bipush 123 // <-- THIS IS YOUR DEFAULT VALUE
istore1 // reference to arg1
}
L1 {
aload0 // reference to arg0
iload1 // reference to arg1
invokevirtual org/guenhter/springboot/kt/Fun test((I)V);
return
}
}
public final test2() { //()V
<localVar:index=0 , name=this , desc=Lorg/guenhter/springboot/kt/Fun;, sig=null, start=L1, end=L2>
L1 {
aload0 // reference to self
iconst_0
iconst_1
aconst_null
invokestatic org/guenhter/springboot/kt/Fun test$default((Lorg/guenhter/springboot/kt/Fun;IILjava/lang/Object;)V);
}
L3 {
aload0 // reference to self
bipush 100
invokevirtual org/guenhter/springboot/kt/Fun test((I)V);
}
L4 {
return
}
L2 {
}
}
因此,您最好的选择是将默认值提取为常量:
private val DEFAULT_MIN = 1
private val DEFAULT_MAX = 1
fun DieRoll.cheatRoll():Int = roll(min = DEFAULT_MAX-1)
fun roll(min: Int = DEFAULT_MIN, max: Int = DEFAULT_MAX): Int = (min..max).rand()
不,它不能。由于参数的默认值可能是一个带有可能副作用的异常表达式,因此在任何地方明确使用它都不是非常清晰和直观的:语义应该是什么,是否应该在每次使用时对其进行评估?是否只将最小值和最大值存储为常量?这也会使访问可见性更好。