I am trying IntDef typedef to restrict the specific type of parameter to the function. But when I am using IntDef using java it's working properly but at the same time, it's not working with kotlin. Below is my code snippet -
In JAVA -
@IntDef({CASH,WITHDRAW})
@Retention(RetentionPolicy.SOURCE)
public @interface TransTypeJava {
int CASH = 1; int WITHDRAW = 2;
}
In Kotlin -
const val CASH = 1 const val WITHDRAW = 2
@IntDef(CASH, WITHDRAW)
@kotlin.annotation.Retention(AnnotationRetention.SOURCE)
internal annotation class TransTypeKotlin
Using in class -
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
demo(TransTypeJava.CASH)// Working
demo(1) // Showing compile time error
}
fun demo(@TransTypeJava type:Int){}
Calling to the demo function gives me an error saying that Must be one of:
TransTypeJava.CASH
, TransTypeJava.WITHDRAW
by passing value other than TransTypeJava
type.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
demo(TransTypeKotlin.CASH) //Working
demo(1) // Working
}
fun demo(@TransTypeKotlin type: Int){}
While using this with kotlin it does not show any error by passing value other than TransTypeKotlin type. Any help will be appreciated.