简体   繁体   English

在Kotlin中访问函数的默认参数值

[英]Accessing a function's default parameter value in Kotlin

Can a function's default parameter value be accessed from a function extension, or anywhere else? 可以从函数扩展或其他任何地方访问函数的默认参数值吗?

fun DieRoll.cheatRoll():Int = roll(min = max -1)

fun roll(min: Int = 1, max: Int = 6): Int = (min..max).rand()

No, this is not possible. 不,这是不可能的。 The default values are not accessible. 无法访问默认值。 They are just contained in a bridge-method in the bytecode: 它们只包含在字节码中的bridge-method中:

fun test(a: Int = 123) {
}

fun test2() {
    test()
    test(100)
}

Results in the bytecode: 结果在字节码中:

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 {
     }
 }

which in Java syntax would look like 在Java语法中看起来像

final public class AKt {
    final public static void test(int i) {
    }

    public static void test$default(int i, int i0, Object a) {
        if ((i0 & 1) != 0) {
            i = 123;
        }
        AKt.test(i);
    }

    final public static void test2() {
        AKt.test$default(0, 1, (Object)null);
        AKt.test(100);
    }
}

So the best alternative for you would be to extract the default value into a constant: 因此,最好的替代方法是将默认值提取为常量:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM