No, It is Immpossible. Default values are not available. They are simply contained in bridge-methodthe bytecode:
fun test(a: Int = 123) {
}
fun test2() {
test()
test(100)
}
Results in bytecode:
public final test(int arg0) {
<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) {
iload2
iconst_1
iand
ifeq L1
L2 {
bipush 123
istore1
}
L1 {
aload0
iload1
invokevirtual org/guenhter/springboot/kt/Fun test((I)V);
return
}
}
public final test2() {
<localVar:index=0 , name=this , desc=Lorg/guenhter/springboot/kt/Fun;, sig=null, start=L1, end=L2>
L1 {
aload0
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
bipush 100
invokevirtual org/guenhter/springboot/kt/Fun test((I)V);
}
L4 {
return
}
L2 {
}
}
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()
source
share